简体   繁体   中英

Modifying view inside a fragment inside other fragment doesn't works

I am trying to modify views inside a fragment1 from other fragment2, parent of fragment1, calling a public method of fragment1, but doesn't works.

In the container fragment a do this:

AddressEditSubviewFragment profesionalEditFragment = new AddressEditSubviewFragment();
        notificationsEditFragment = new AddressEditSubviewFragment();
        fragmentTransaction.add(R.id.addresses_edit_fragment_notifications_edit_fl, notificationsEditFragment);
fragmentTransaction.commit();

CheckBox notCB = (CheckBox) view.findViewById(R.id.addresses_edit_fragment_notifications_same_cb);
        notCB.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                notificationsEditFragment.disableEnableEdit(true);

            }
        });

The method in the fragment to change the state of the views is this:

public void disableEnableEdit(boolean disable) {

        streetET.setKeyListener(null);
        streetET.setCursorVisible(false);
        streetET.setPressed(false);
        streetET.setFocusable(false);
        numberET.setFocusable(!disable);
        numberET.setEnabled(!disable);
        numberET.setText("pruebas");
        floorET.setVisibility(View.GONE);
        buildingET.setFocusable(!disable);
        buildingET.setEnabled(!disable);
        pcET.setFocusable(!disable);
        pcET.setEnabled(!disable);



    }

When I call the method from the container fragment it enters in the method, but not change nothing. Why is this thing happens?

you can't and shouldn't have fragments communicate directly with one another. all communication between them must go through the activity or the parent fragment (if it's hosting both of them).

There are a few ways to do this. here's one method .

I personally prefer working with broadcastreceivers . The method is basically:

  1. Have fragment1 call some method in the activity.
  2. Have that method sent a broadcast on some intent filter when its done.
  3. Implement a broadcast receiver on fragment2, that does stuff when receiving a broadcast.
  4. Have fragment2 register and unregister the receiver on said intent filter as you please (usually done on the onResume and onPause respectfully).

Tell me if my explanation wasn't clear enough, I'll provide a working code example.

Good luck! :)

--- EDIT ---

Ok, after understanding you're trying to call a method in a child fragment from its parent fragment, here's a method with broadcast receivers that should work:

parent fragment sends a broadcast on some intent filter whenever you need to call the method in the child fragment.

Intent intent = new Intent();
intent.setAction(some_string);
getActivity().sendBroadcast(intent);

implement a broadcast receiver in child fragment that calls that method in its onReceive() .

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        doStuff();
    }
};

register the receiver to the intent filter in the child fragment's onResume() , and unregister it in the child's onPause() .

getActivity().registerReceiver(receiver, new IntentFilter(some_string));
getActivity().unregisterReceiver(receiver);

Finally, I found the problem. The problem was that I had two cheboxes whit the same id in the parent fragment. And when I check a check box doesn't work well. Sorry for the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM