I’m working on an app that uses a custom class with other custom classes as members. ie:
public class TopClass {
public OtherClass1 otherClass1;
public OtherClass2 otherClass2;
}
This data needs to be available to several other Activities. I had been storing this in the main Activity of the app and was able to reference it.
Everything is working great except when running on a Droid X2. On that device it will run fine – until an external Intent is called. The same thing happens when switching to the Camera, Maps, or Calendar Intents: after returning to the app with either the back button or, in the case of camera, the “OK” button the app will FC with NPE referencing the “TopClass”
Here is an example of the camera call for reference:
public void startCamera(int pos) {
Log.v(App.DEBUG_TAG, "startCamera(slot "+pos+")");
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
i.putExtra(MediaStore.EXTRA_OUTPUT, getFilesDir().getAbsolutePath());
i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(i, pos);
}
I switched to using an Application class to store the “TopCLass”, but no change.
Using ACRA to get the stack traces and logcat where I’ve started logging every lifecycle event in the Activities I can see that the “TopClass” is null upon returning to the Activity. All of the local variables for the last Activity still exist in unchanged states, just the ‘global’ data has been nullified – GC’d, I am guessing.
I can’t duplicate this on my device (Droid Incredible running CM7 [2.3.7]), or any emulator – not even the X2 addon, nor on my wife’s Droid X.
I’m trying an experiment where I copy the ‘global’ “TopClass” to a local “TopClass” variable, but haven’t heard back on whether or not that has worked yet.
My questions are:
-
What would be the proper or recommended way of persisting this data? From my reading I gathered that it was to use an Application class instance, but that isn’t working.
-
Any ideas why the X2 is nulling out the Application-owned classes when calling an external Intent?
You persist data in a file. Whether that is a database,
SharedPreferences, or a file of your own design is up to you.Most likely, it is not “nulling out the Application-owned classes”. Rather, your process is being terminated. Any time you are not in the foreground, your process may be terminated to free up memory for other processes, whether you are not in the foreground because you launched a third-party activity, or the user pressed HOME, or the user brought up the recent tasks list and navigated to another task, or the user received a phone call, and so on. It is possible that the X2 is more aggressive about killing off background processes than are other devices, whether specifically by design or as a side-effect of other decisions made by Motorola.