How to save the image on screenSaveInstanceState when rotating the screen?

I am loading an image into AsyncTask image viewer and I want to save my image when I rotate my phone rather than loading it again into the image ...

public void onSaveInstanceState(Bundle toSave) {
    super.onSaveInstanceState(toSave);
    my_image.buildDrawingCache();
    Parcelable bm = my_image.getDrawingCache();
    toSave.putParcelable("savedImage", bm);

} 

      

I am trying to figure out if I am doing something wrong with onSaveInstanceState or not and how to get this onRestoreInstanceState and put the image without reloading ...

+3


source to share


3 answers


The onSaveInstanceState () function should be used to save small objects, not heavy objects.

If you want to preserve heavy images when you rotate your phone, use any of the following methods:

  • If you want to keep large objects, use the RetainNonConfigurationInstance () function.
  • Or else we can make this image static so that the image is loaded only once. Meaning: When loading an image from the network, make it specify a static variable. If the user turns the phone, as android is killing this activity and re-creating it, just put an if condition if this static variable is not null, then load again. As you know, static variables will only be created once, it won't be loaded again.


But it is advisable to switch to the 1st option.

Ref: official android developer guide

+6


source


First of all super.onSaveInstanceState(toSave)

should be in the last place in the method onSaveInstanceState(Bundle toSave)

.

In onCreate(Bundle savedInstanceState)

check if is savedInstanceState

not null and gets what you want:



@Override
protected void onCreate(Bundle savedInstanceState) {
...
    if(savedInstanceState != null){
        ...
        ...savedInstanceState.getParcerable(...);
        ...
    }
}

      

0


source


Don't use static variables to bind to bitmaps.

0


source







All Articles