androidimageviewscreen-orientationandroid-configchanges

strange behavior of image view on changing the orientation of the screen


i am using an imageView to display an image. later i am setting a different image to the view using imageView.setImage(bitmap).now if i change the orientation of the screen from portrait to landscape, the imageview displays the old image instead of the new image i have set. can someone pls tell me why this happens and how to overcome this.


Solution

  • Normal case when you change you orientation the activity will recreate(call onCreate()).
    If you have only one xml for both orientation, you can block this by
    1. Set the activity as android:configChanges="orientation" in manifest file

    <activity android:name=".Youractivityname" 
          android:configChanges="orientation"/>
    

    2. Then override this in our activity class

    public class Youractivityname extends Activity {
    
         public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                // all your codes 
          }
    
        @Override
            public void onConfigurationChanged(Configuration newConfig) {
              super.onConfigurationChanged(newConfig);
    
            }
    }
    

    Hope now it is clear for you