简体   繁体   中英

Sending information from one fragment to another Android

I am having this issue where I have nested fragments. I need one to be able to send information to the other.

For example, if I press a button in the inner fragment I want the outer fragment to perform an action.

Any help would be great!

Edit: I forgot to mention that both of these are live fragments. As far as I know using bundles only allows me to set arguments when creating a fragment but not after it has been created.

Well there are plenty of ways to manage this. It really depends on the architecture you want to go with.

You could go lazy and use a Bus library that uses sub/pub modeling and reflection for notifying generic objects that registered and have matching signature methods of changes, However, this is frowned upon to use reflection for regular coding practices that could be implemented without it.

I would say you can either create an interface that represents the calls that would go back and forth like IMainActivityCallbacks with methods like dataChange(myObject obj) . Then in your onAttach of fragment you cast the context as IMainActivityCallbacks. Make sure the MainActivity implements the interface.

Then simply call the mIMainActivityCallbacks.dataChange(someObject); This will call the method in the parent activity and allow him to pass it to the child fragment that needs it with mMyOtherFragment.publicMethod(newDataJustReceived).

You could also get crazier if you want and pass an interface into the child fragment that references the outer fragment so when MainActivity news up the children fragment, the outer one could implement an interface of doStuff and then be passed as an interface to the inner child fragment. So the child fragment can easily say if "mParentInterface" is not null then mParentInterface.doStuff.

There are millions of ways to skin a cat, but prefer to use interfaces rather then Reflection based libraries typically. Hope that helps.

Use interface in this case. 1) Define interface in you first fragment(in which you click something and waiting something to happen in a second fragment) and use callback method you define in your interface. For example:

OnButtonClickListener mCallback;
......your code
public interface OnButtonClickListener {
    void onButtonSelected(int position);
}
......your code
then you call your callback method
.....
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int   position, long l) {
            mCallback.onButtonSelected(position);
        }
    });

2) Then implement this interface in host activity you have for nested fragments.

For example:

public class MainActivity extends AppCompatActivity implements YourFragment.OnButtonClickListener{....

3) Define callback method of your interface in this activity. For example:

public void onButtonSelected(int position) {
...Here just change something in your second fragment.....

Hope this will be helpful.

I use greenrobot EventBus in that cases. You need to add this library to your build gradle:

compile 'org.greenrobot:eventbus:3.0.0'

That you can send message like this:

EventBus.getDefault().post(new ActionEvent("test action"));

And another fragment can catch it this way:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onActionEvent(ActionEvent event) {
    //do something
}

You can sending different actions, if you want, same way. Don`t forget to according to fragment lifecycle:

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

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