Using static variables/classes is a bad approach in terms of maintainability and debugging.
I have been using Activity.onRetainNonConfigurationInstance
but I found out just now that this is deprecated (probably since honeycomb or later).
Activity.onRetainNonConfigurationInstance
Using this method, just call Activity.getLastNonConfigurationInstance
to retrieve the same object you returned in the onRetainNonConfigurationInstance
. Be sure to check for null and cast to the right class (you can return/get any class). Activity.getLastNonConfigurationInstance
A sample usage in pseudo-code would be:
onRetainNonConfigurationInstance:
return "I need to remember this next time";
onCreate:
...
String messageToShow = null;
Object data = getLastNonConfigurationInstance();
if(data != null)
messageToShow = (String)data;
else
messageToShow = "Nothing to show";
So, if you are targetting up to 2.x.x you can use that method. Otherwise, google recommends you to use Fragment.setRetainInstance
. This is backwards compatible via the compability package.
Fragment.setRetainInstance