Android Imageview displays old bitmap after screen rotates -


my app supposed first displays image, when button pressed, image displayed in image view. however, when screen rotated, imageview displays old image. how should go fixing this? (bits bitmap loaded imageview on create)

my code below:

    rgbtobitmap(rgb.getwindow(), bits); //this loads new image bits      imageview.setimagebitmap(bits); 

i suppose setting first image of imageview in oncreate or onstart method of activity.

upon rotating screen, oncreate , onstart methods called again, , therefore imageview displays first image again.

in order save activity state, have @ this: http://developer.android.com/reference/android/app/activity.html#savingpersistentstate

this possible solution:

bitmap image = null;  @override protected void oncreate(bundle savedinstancestate) {  super.oncreate(savedinstancestate);   image = (bitmap) getlastnonconfigurationinstance();       if(bitmap == null){          image = downloadimage();       }      setimage(bitmap); }   @override public object onretainnonconfigurationinstance() {     return bitmap; } 

you can read method when ur using fragments:

fragment.setretaininstance 

or bundle: (onsaveinstancestate(bundle) available in activity)

//save onsaveinstancestate:    @override   public void onsaveinstancestate(bundle tosave) {     super.onsaveinstancestate(tosave);     tosave.putparcelable("bitmap", bitmap);   } //nd oncreate:   @override   public void oncreate(bundle savedstate) {     super.oncreate(savedstate);     if (savedstate != null) bitmap = savedstate.getparcelable("bitmap");   } 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -