开发者

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

开发者 https://www.devze.com 2023-02-04 13:04 出处:网络
i am using an imageView to display an image. later i am setting a different image to the view using imageView.setIma开发者_JAVA百科ge(bitmap).now if i change the orientation of the screen from portrai

i am using an imageView to display an image. later i am setting a different image to the view using imageView.setIma开发者_JAVA百科ge(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.


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


By default on orientation change the activity is destroyed and recreated, so if you set the first bitmap on create, and then change to the second bitmap elsewhere, when the orientation changes the first image is set in the onCreate but if the code that changes image isn't called again then the image doesn't change.


Using android:configChanges="orientation" in the manifest is a bad practice. This is the recommended way to handle the orientation change.


I had a problem while changing the screen portrait to landscape the image is hide .I used the line in AndroidManifest.xml in activity below my problem was solved

 android:configChanges="orientation|screenSize"
0

精彩评论

暂无评论...
验证码 换一张
取 消