I’m creating an application that has a Gallery, and this Gallery has a different number of Views depending on orientation. Broadly, some wide images are double-page spreads; as such they take up one View in landscape, but they are split to two Views (one per page) in portrait.
I have a system that remembers which “page” is selected and converts this to a position that can be sent to Gallery.setSelection(position). I’m calling this from Activity.onCreate(), using Activity.onRetainNonConfigurationInstance() to pass the object holding this, among other, information back to the application. Unfortunately Gallery then seems to be resetting itself to its previous position value after onCreate finishes.
Can I suppress this? If not, when do I need to call my Gallery.setSelection(position) method?
I worked it out.
When the orientation changes,
Activity.onCreate()is called and everything is inflated. By overridingActivity.onResume()and calling Gallery.setSelection(position) with the desired value after super.onResume(), it is possible to get the correct behaviour; presumably the Gallery’s selection is set to its previous value either during super.onResume() or in between Activity.onCreate() andActivity.onResume().