Skip to content Skip to sidebar Skip to footer

Send Data From One Fragment To Another Using Bundle . I Tried This. It's Not Working

I tried the code below, but it's not working . program crushed without giving me output. How can i send data from one fragment to another fragment in same activity? First time usin

Solution 1:

It is not recommended way

All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

Suggestion

Take activity instance from onAttach() inside fragment, and then ask Activity to communicate to another fragment.

Ref: Android Documentation https://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

Solution 2:

In FirstFragment create Bundle like this

Bundlebundle=newBundle();
bundle.putString("key","abc"); // Put anything what you wantSecondFragmentfragment2=newSecondFragment();
fragment2.setArguments(bundle);

getFragmentManager()
      .beginTransaction()
      .replace(R.id.content, fragment2)
      .commit();

In SecondFragment

Bundlebundle=this.getArguments();

if(bundle != null){
     // handle your code here.
}

Hope this helps you.

Post a Comment for "Send Data From One Fragment To Another Using Bundle . I Tried This. It's Not Working"