0

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.

Steeven_b
  • 747
  • 2
  • 10
  • 22

1 Answers1

2

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.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46