My app functions like this: an imageView that can be visible in portrait or landscape, and the user can swipe between images to change the image in the imageView. The issue is that if you swipe to select a different image and then rotate the device to landscape it shows the original image the user selected, not the current image that was being displayed in the imageView. I am wondering how I can get the id of the current image before it is rotated, and then set it for when it is in landscape. Would it be in the onPause method, or is there a onConfigurationChanged method I could use for this?

有帮助吗?

解决方案

See here: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

According to the documentation, the activity is destroyed when the orientation changes. In order to save additional state information in such an event, you can override the onSaveInstanceState() method and add key-value pairs to the bundle object, like so:

public void onSaveInstanceState(Bundle savedInstanceState){
    savedInstanceState.putInt("CurrentImageId", [current image id here]);
    super.onSaveInstanceState(Bundle savedInstanceState);
}

Then, when the application is restored after the orientation change, you can retrieve the current image's id by overriding either the onCreate(Bundle savedInstanceState) method or the onRestoreInstanceState(Bundle savedInstanceState) method:

public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    int currentImageId = savedInstanceState.getInt("CurrentImageId");
    //Set the current image here, after retrieving the id
}

Of course, I don't know exactly how you're identifying your images, so I can't tell you exactly how to set/retrieve the current image's id.

其他提示

Try to add android:configChanges="orientation" below <activity> in your AndroidManifest.xml

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top