Android Fragments: how do I save and pass data back to parent activity once the user closes the current activity?

StackOverflow https://stackoverflow.com/questions/21573511

Domanda

I have a SearchFragment and a PersonFragment which are hosted by different FragmentActivitys

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.

È stato utile?

Soluzione 4

Ended up using EventBus for this

Altri suggerimenti

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()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top