Ended up using EventBus for this
Android Fragments: how do I save and pass data back to parent activity once the user closes the current activity?
-
07-10-2022 - |
Question
I have a SearchFragment
and a PersonFragment
which are hosted by different FragmentActivity
s
The user will navigate from the SearchFragment
to the PersonFragment
. When the user is done with the PersonFragment
(such as when they press the back button), I would like to send data back to SearchFragment
so it can update its UI with any changes the user made while in PersonFragment
.
I read the best way to do this is in the Activity's finish()
method. However, since I'm using fragments I'm not sure how to accomplish this.
La solution 4
Autres conseils
You first communicate from PersonFragment
to the associated activity using interface as a callback.
Then you can use intent to pass values between activities. From activity you can communicate to SearchFragment
.
http://developer.android.com/training/basics/fragments/communicating.html
According to the tutorial on the Android developers site (link), you should declare a listener interface in your PersonFragment, implement it in your FragmentActivity and in that implementation, update your SearchFragment's UI.
Edit: Another approach would be to use LocalBroadcastManager, if you can package your relevant data in an Intent, this approach uses a bit less boilerplate code.
you dont need it, all you need to do is some ContentObserver that notifies you about the content changes, see ContentResolver and its methods: registerContentObserver() and notifyChange()