sat
sat

Reputation: 41076

Saving member variables when transitioning to another activities

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.

Upvotes: 0

Views: 445

Answers (4)

Lumis
Lumis

Reputation: 21639

I would not try to save the enitre array list with objects. It is not very likely that the activity in the background would be killed, what it may happen is that some bitmaps are destroyed by the system because they take a lot of memory. This may happen when one clicks on home button or the device locks out the screen with the security password.

So in on activity resume one could check if bitmap references are null and then reload bitmaps if needed. One can also test if the objects in the arrayList still exist.

I am not aware of a way to save a class object into the permanent memory like preferences, applicaiton directory or SD, only basic types of data can be saved. It would be a huge job to serialise object properties in order to save them.

Upvotes: 1

Mister Smith
Mister Smith

Reputation: 28168

If the data is an instance variable of your activity, it is not going to be GCed unless the whole Activity is destroyed by the OS. A good way of testing this is to tilt the emulator to landscape mode and see how the activity is recreated (unless you configured your app to ignore this event).

To save the data, you have several ways to do it, from saving it on an Application instance variable, to use Bundles, or use persistence. Check the docs:

http://developer.android.com/guide/topics/resources/runtime-changes.html

Upvotes: 0

inazaruk
inazaruk

Reputation: 74790

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 your onPause(). 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 in onPause()/onDestroy() (and then restore it in onCreate()) you'll have empty field after config change.

Upvotes: 3

Mark Allison
Mark Allison

Reputation: 21909

The official docs for android.app.Activity states:

When an activity's onPause() method is called, it should commit to the backing content provider or file any changes the user has made.

On this occasion Activity1 has not been GC'd but on another occasion it might. On a pre-Honeycomb system, an Activity in "paused" state may be killed by the OS if resources run short. To ensure that it's state is always maintained you should persist data on onPause() and reload it in onResume().

Upvotes: 1

Related Questions