Patrik Berger
Patrik Berger

Reputation: 81

Android, manipulating previous activity

I have got 2 activities - "A" contains fragment with a list of conversations, "B" represents a conversation (shows it's messages), in B I can also send a new message. I would like to be able to update A's list of conversations every time a message is sent in B, so when I click android's back button, A activity's view is updated.

The core of the problem for me, is that I'm not starting A through an Intent when I click the android's back button, so I don't know how to get this effect.

Thank you for your help!

Upvotes: 1

Views: 70

Answers (1)

Kevin Krumwiede
Kevin Krumwiede

Reputation: 10288

When A is on the backstack, there's no guarantee that an instance of A even exists in memory. The answer to the question of how to manipulate A from B is don't.

Some correct ways of doing it:

  • If your model (the list of conversations) is Parcelable or Serializable, you can pass it between activities via Intent. You can pass it back from B to A if you start B for result and retrieve it from the Intent returned to A's onActivityResult.
  • Make the model persistent, like in a database or SharedPreferences file.
  • Put the model in a bound Service. This would be faster than having each activity load it from persistent storage, but you may still need to make it persistent so you don't lose it when the Service shuts down.

Upvotes: 1

Related Questions