Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass result from second fragment to first fragment

Tags:

In my app I have two fragments say fragmentA and FragmentB. When I click on a button in fragmetA, a list is opened in fragmentB. Now when I select an item from list in fragmentB I want the result to be passed to fragmentA. I am using only one TabActivity for all fragments. When list item is selected in fragmentB I am popping out fragmentB from stack so that I can directly go back to fragmentA.

Does anyone knows how to pass result to previous fragment.

Thanks.

like image 573
abhishek Avatar asked Aug 24 '12 06:08

abhishek


People also ask

Can you pass the data between two fragments?

When working with child fragments, your parent fragment and its child fragments might need to share data with each other. To share data between these fragments, use the parent fragment as the ViewModel scope.

How send data from second fragment to first fragment in Android?

In one fragment activity, call a method and pass a variable to the main activity. From the main activity you can send it to your other fragment activity if you'd like. Show activity on this post. You can also use SharedPreferences to save some string and after return back to the first fragment load it and clear.

How you can pass the data from one fragment to another fragment?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken fragments to pass the data between two fragments.

How do I pass data between Viewpager fragments?

The best solution in my opinion is, if you can store the data in a DataBase, the each Fragment store the data inside the database via the CotnentProvider then you could use Loaders specifically CursorLoader to retrieve the data - this approach will always provide you with the latest data as the Laoder will be notified ...


1 Answers

Update

Activity is the parent controller and should take responsibility for handling those events raised by its fragments/views, which concern something outside of the scope of fragment/view itself.

A Fragment is to act as a sub-controller of Views it hosts. All the events and communication between its own views, the fragment should handle itself. When there is an event outside of a fragment's scope and responsibilities (like sending data to another fragment), that event should be escalated to its parent controller, the Activity.

enter image description here

Old

From this tutorial : http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Its better to let the activity apply changes to its fragment than passing values directly between fragments. Let your Activity implement a FragmentListener interface with onQuery(Bundle data) and onResult(Bundle data) methods.

Create a FragmentListener varaible in each of your fragments and then override onAttach() of each fragment as:

 public void onAttach(Activity activity) {     super.onAttach(activity);      //---register parent activity for events---     try{         fragmentListener = (FragmentListener) activity;     }catch (ClassCastException e)     {         throw new ClassCastException("Parent activity must implement interface FragmentListener.");     }   } 

This will enforce your child fragments to be automatically registered to parent Activity.

Also, remember to release fragmentListener reference in onDetach().

Now you can call your Activity from fragments.

On the other side, your Activity can always search for a fragment using getFragmentManager().findFragmentByTag("fragmentA") or findFragmentById("FragmentA"). If it can find your Fragment, Then it can cast it into your FragmentA class and call its methods. Same can be done with FragmentB or any other fragment..

like image 127
S.D. Avatar answered Nov 09 '22 00:11

S.D.