问题
I'm working on an Android app, I get some datas in a fragment and I want to use them in an other fragment. So I was looking to create a bundle, get it in the activity with a simple getBasicInfos() method that return my bundle then send it to my other fragment. But the problem is I can't use this method in my activity.
fragment = new DashboardFragment();
fragment.getBasicInfos(); //Does not recognize the method
toolbar.setTitle(getResources().getString(R.string.dashboard));
break;
I want to know if there is a better way, or more simple.
回答1:
Create Interface
in Fragment
and implement that interface in Activity
Then while Instantiating the fragment = new DashboardFragment(this);
pass this as the listener and in Fragment constructor
save this
public DashboardFragment(FragmentListener listener) {
this.listener = listener;
}
And then use this listener to pass the data to your activity
.
hope this helps.
来源:https://stackoverflow.com/questions/35038358/android-send-data-from-fragment-to-activity