Android Hashmap Not Persisting When Returning To Activity
I am trying to keep my HashMap values when I navigate to another activity and return. This is the code I have for now. The HashMap works and is able to grab and save the data from
Solution 1:
However as soon as I leave from the activity and return, the HashMap is reinitialized to empty -> {}
If by "leave from the activity and return", you mean press the BACK button, then do something to start a fresh activity... then your behavior is expected.
The Bundle
for the saved instance state is used in two main scenarios:
- Configuration changes (e.g., user rotates the screen)
- Process termination, and the user returns to your recent task (e.g., via the overview screen)
Pressing BACK to destroy the activity is neither of those. Hence, the state is not saved.
If this HashMap
represents model data — the sort of data that you expect to be able to get back to, time and again, no matter how the user uses your app — save it to a database, SharedPreferences
, other sort of file, or "the cloud".
You can read more about these scenarios in the Activity
documentation.
Post a Comment for "Android Hashmap Not Persisting When Returning To Activity"