Scenario :
I have a member variable, an array-list,(which is not static, private member) which contains custom objects in Activity1,
I transition to Activity 2, putting activity1 in pause state.
As of now I am not saving data of array-list.
I come back from activity 2 , and activity one is in resume state.
My doubt is, do I have to save the data of array-list(member variable) in onPause() when I am moving to activity2, essentially , array-list is holding references to custom object.
Though with quick test, data of array-list is still intact, but I am doubtful whether those objects are free to be garbage collected and that I should save the data of array-list and reload it in onResume.
It’s better to save your data in
onPause()because this is last method that’s guaranteed to be called, after that there is no guarantees for any of the Activity’s lifecycle methods to be called. See more in “Activity Lifecycle”.If data in Activity member should not be persisted across process starts, you can save it in your
onDestroy()too. If process is not killed,onDestroy()is also guaranteed to be called.But you should be aware that as soon as Activity 2 is started, Activity 1 can be destroyed at any time. So you probably need to persist you member object somehow at least in
onDestroy(), and even better in youronPause(). Another thing to be aware of is configuration changes (orientation change for example). Those trigger destruction of old Activity and creation of new Activity instance. If you do not persist you member inonPause()/onDestroy()(and then restore it inonCreate()) you’ll have empty field after config change.