Question

I have an application Activity that in onCreate loads an XML file from a service using an AsyncTask. The XML is parsed into an ArrayList. When I switch to a different activity and then back to the main activity, I want to be able to recognize that that XML file was already loaded and use the populated ArrayList.

What is the best way to persist that ArrayList?

onSaveInstanceState only seems to support primitives and I've been unable to set up a case where onRetainNonConfigurationInstance actually gets called. So in onCreate, the XML data is loaded from the server ever time I switch to that Activity. I have made the models that are in the ArrayList implement Parcelable, so could use that in some way?

Was it helpful?

Solution

What is the best way to persist that ArrayList?

I don't see where your problem has anything to do with multiple activities. What happens if the user presses HOME (gasp!), for example? Your app will eventually be closed. Do you want to reload the data from the server? If the answer is "yes", then you don't need to "persist" anything, and onSaveInstanceState() may suffice (see below). If the answer is "no", then you need to rethink your approach to your data model, so you arrange to keep the data in a database, synchronizing with your Web service periodically, and probably dumping the ArrayList and replacing it with a Cursor.

onSaveInstanceState only seems to support primitives

If the answer to my HOME question is "yes", then you can just hold onto the data in a data member of your activity, and, if it is modestly sized, also stash it in the Bundle in onSaveInstanceState(). A Bundle can hold an ArrayList of Parcelable. However, if the data set is large (say, 100KB or more), you probably don't want to go this route and should consider the "no" path I described above.

I've been unable to set up a case where onRetainNonConfigurationInstance actually gets called.

Rotate the screen. There are other scenarios, but orientation changes are the easiest ones to trigger it.

However, it has nothing to do with your problem.

OTHER TIPS

"onSaveInstanceState only seems to support primitives"

onSaveInstanceState supports objects, as long as they are declared serializable.

// ON_SAVE_INSTANCE_STATE
// save instance data (5) on soft kill such as user changing phone orientation
protected void onSaveInstanceState(Bundle outState){
    password= editTextPassword.getText().toString(); 
    try {
        ConfuseTextStateBuilder b= ConfuseTextState.getBuilder();
        b.setIsShowCharCount(isShowCharCount);
        b.setTimeExpire(timeExpire); 
        b.setTimeoutType(timeoutType);
        b.setIsValidKey(isValidKey); 
        b.setPassword(password);
        state= b.build(); // may throw
    }
    catch(InvalidParameterException e){
        Log.d(TAG,"FailedToSaveState",e); // will be stripped out of runtime
    }
    outState.putSerializable("jalcomputing.confusetext.ConfuseTextState", state);  // save non view state
    super.onSaveInstanceState(outState); // save view state
    //Log.d(TAG,"onSaveInstance");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top