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?
2 Answers
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.

- 363
- 2
- 8
-
I understand the logic, now I am just having a hard time getting the drawable reference from the imageView. I tried to use imageView.getId() but that returned 0. Is there a function to get the drawable id from the imageView ? I am hoping to get something like R.drawable.imageX – ez4nick Jan 05 '14 at 23:41
-
using getDrawable leads to this: putInt (String,int) in Bundle cannot be applied to (String, android.graphics.drawable.Drawable) – ez4nick Jan 06 '14 at 00:21
-
Yeah, you cannot easily get the id of a drawable after the fact. Instead, try to keep track of which drawable is currently displayed by storing a reference to it at the time that the drawable is set. EDIT: Are the images you are displaying in the drawable resources folder, or are they files? If they aren't in the drawable resources folder, they won't have the kind of id you're looking for. If they're files, for instance, try to store the path name. – Quercus Jan 06 '14 at 00:24
-
The images are in the drawable resource folder. I think I have figured it out but I have 1 more confusing part, after rotating to landscape and then back to portrait, what method would that be under? It seems that rotating to landscape and then back to portrait still reverts to the original image although it was on the correct image after the first orientation change – ez4nick Jan 06 '14 at 00:34
-
Hmm, you mean that it now works for the first orientation change, but not the next? That's very strange. I'm not sure why that would be the case... – Quercus Jan 06 '14 at 00:43
-
Yes, I'm not sure why this is the case either. I will have to see if there is a better way to accomplish this – ez4nick Jan 06 '14 at 00:48
Try to add android:configChanges="orientation"
below <activity>
in your AndroidManifest.xml

- 913
- 10
- 32
-
Tried that and it did not work, still reverts to original image selected – ez4nick Jan 05 '14 at 23:12