1

I've imported some JSON data and fetched it into a custom arrayList. The arraylist looks like this:

ArrayList<HashMap<String, String>> postList;

It all works fine, but if the orientation changes the whole JSON parse/fetch process will start over again. That's why I like to store my custom ArrayList.

I have found a lot of information about (how to use) the Parcelable interface, but they all cover sending data from activity A to B.

Can somebody please provide an example about how to use a Parcelable inside the same activity? Thanks in advance!

Foo
  • 596
  • 2
  • 5
  • 21

2 Answers2

0

You should search online for onSaveInstanceState, it's a callback which is defined inside Activity class.

It works in the same way when you need to pass data between activites

@Override
protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
}

Inside outState you will use normal Bundle methods to pass Strings, Integers and Parcelable values (putParcelableArrayList/putParcelable/putParcelableArray).

When the screen rotate or something happens (activity got destroyed etc.), this method could be called and when the activity gets recreated the bundle you used here will be passed to onCreate inside the savedInstanceState argument (the only argument of onCreate, Bundle).

Here inside onCreate you check if (savedInstanceState != null) to be sure you have a saved state, if it's true you use savedInstanceState.getParcelableArrayList (or something else, depends on what you want to get) to read back the list.


if (savedInstanceState != null)
{
    ArrayList<Parcelable> parcelableArrayList = savedInstanceState.getParcelableArrayList("key");
}

@Override
protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);

    outState.putParcelableArray(myParcelableList);
}
Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • Thanks for tje answer, Ill try this solution and keep you posted. – Foo May 19 '14 at 22:01
  • I get this feedback from eclipse: The method putParcelableArray(String, Parcelable[]) in the type Bundle is not applicable for the arguments (ArrayList>) – Foo May 19 '14 at 22:05
  • Use putParcelableArrayList. but i don't think it will work. Maybe use an inner class to incapsulate HashMap. Or choose a method which fits best for you: http://developer.android.com/reference/android/os/Bundle.html -- See this – Marco Acierno May 20 '14 at 07:48
0

It all works fine, but if the orientation changes the whole JSON parse/fetch process will start over again. That's why I like to store my custom ArrayList.

You mentioned that your orientation causes the whole process to restart in your Activity. Are you running the json... etc in your onCreate method? If that's the case, orientation changes cause onCreate to be called.

You can prevent onCreate from being called by modifying your AndroidManifest.xml to handle your own orientation changes for the current activity (look for the android:configChanges):

<activity
android:name="your.activity.package.andclass"
android:configChanges="orientation|screenSize">
<!-- etc -->

You might need to manually handle config changes for certain events, but I've never found it necessary.

Ani Fichadia
  • 267
  • 2
  • 10