Android send Data from fragment to Activity

笑着哭i 提交于 2021-02-11 15:57:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!