azizbekian
azizbekian

Reputation: 62179

Saving Arraylist<Hashmap> for displaying after rotation

I can I retain ArrayList<HashMap<String, String>> in bundle?

I want to display ListView immediately after rotating the screen.

Upvotes: 1

Views: 1275

Answers (2)

pjco
pjco

Reputation: 3836

You dont have to save it in the Bundle. In fact, that seems like a lot of info to store in a Bundle.

Since you are just saving a HashMap of String objects, you could make a class to cache it for you or make it a static field in the class.

Static fields are fine to use as long as they dont contain a reference to the Activity or a View or Drawable. (That can cause memory leaks)

The simplest way you could do this might just be:

private static HashMap<String,String> myMap = new HashMap<String,String>();

Because this field is static, it will not be recreated when your activity is recreated. Rather, it will still be there for you to use with the same values.


More ways to store data:

(These techniques apply to sharing data over a configuration change also).

http://developer.android.com/guide/faq/framework.html#3

How do I pass data between Activities/Services within a single application?

It depends on the type of data that you want to share:

Primitive Data Types

To share primitive data between Activities/Services in an application, use Intent.putExtras(). For passing primitive data that needs to persist use the Preferences storage mechanism.

Non-Persistent Objects

For sharing complex non-persistent user-defined objects for short duration, the following approaches are recommended:

Singleton class

You can take advantage of the fact that your application components run in the same process through the use of a singleton. This is a class that is designed to have only one instance. It has a static method with a name such as getInstance() that returns the instance; the first time this method is called, it creates the global instance. Because all callers get the same instance, they can use this as a point of interaction. For example activity A may retrieve the instance and call setValue(3); later activity B may retrieve the instance and call getValue() to retrieve the last set value. A public static field/method

An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.

A HashMap of WeakReferences to Objects

You can also use a HashMap of WeakReferences to Objects with Long keys. When an activity wants to pass an object to another activity, it simply puts the object in the map and sends the key (which is a unique Long based on a counter or time stamp) to the recipient activity via intent extras. The recipient activity retrieves the object using this key.

Persistent Objects

Even while an application appears to continue running, the system may choose to kill its process and restart it later. If you have data that you need to persist from one activity invocation to the next, you need to represent that data as state that gets saved by an activity when it is informed that it might go away.

For sharing complex persistent user-defined objects, the following approaches are recommended:

  • Application Preferences
  • Files
  • contentProviders
  • SQLite DB

If the shared data needs to be retained across points where the application process can be killed, then place that data in persistent storage like Application Preferences, SQLite DB, Files or ContentProviders. Please refer to the Data Storage for further details on how to use these components.

Upvotes: 4

Wizetux
Wizetux

Reputation: 756

You should probably be overriding the Activity or Fragments onSaveInstanceState(Bundle) function. This will allow you to set any data that you wish to save between instances of the same Activity or Fragment. This is the correct way to save between rotations.

** Updated **

Since you have an ArrayList that contains 1 or more Hashmaps, you might need to save two objects into the bundle. One being the number of HashMaps that are in the array, and then each HashMap. Since the HashMaps are Serializable, you can just use the Bundle.putSerializable to place each of the HashMaps into the bundle. You will need to place the needed information into the bundle in order to re-create the ArrayList in the OnResume().

One method might be this: Have the keys to each HashMap be some string like "Hashmap1", "Hashmap2" and so on for each HashMap. Then if you know the number of HashMaps to retrieve from the bundle and the key format, then you can recreate the ArrayList.

Upvotes: 0

Related Questions