Saving some orientation data in Android

As I understand it, your Android activity will be recreated for a new orientation with any orientation changes.

Is there a way to save / save some data from the original orientation when changing the orientation?

I would like to save some bitmaps, so I don’t need to load them again when the orientation changes.

+5
source share
5 answers

Using static variables / classes is a poor approach in terms of maintainability and debugging.


Activity.onRetainNonConfigurationInstance, , (, ). Activity.onRetainNonConfigurationInstance

, Activity.getLastNonConfigurationInstance, , onRetainNonConfigurationInstance. null ( / ). Activity.getLastNonConfigurationInstance

:

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";

, 2.x.x, . Google Fragment.setRetainInstance. compability.

Fragment.setRetainInstance

+12

Android , .

, T-Mobile G1, . , , .

Android, Android .

[...]

, , Android, , .

"" , , , , .

[...]

.

+1

. Android- . , .

0

you can try using sharedpreferences:

editor edit = preferences.edit(); edit.putString("username", "new_value_for_user"); edit.commit();

0
source

All Articles