Question

Because of theming issues, I am using a custom view on the Actionbar, a Spinner. When the user selects a certain item, and then clicks a button, it changes the Actionbar to two buttons, Done/Discard, as Roman Nurik explains here: https://plus.google.com/+RomanNurik/posts/R49wVvcDoEW . Using Otto, the Activity is kept informed when that Fragment is stopped, so it can revert to the normal Actionbar with the Spinner. However, the Spinner doesn't retain selection - if "Manage stores", for example, was selected before clicking the button that changes the Actionbar, when it is "restored", "Manage stores" should be kept selected.

Currently I am using savedInstanceStates to save the selected item, but of course that only works for the "screens" that have the Spinner, and only works for application restarts or device rotation.

Was it helpful?

Solution

In your Spinner's onItemSelectedListener, save the selected position.

You can store the position in the SharedPreferences if you want the position to persist outside of the Activity's scope and after application restart. Or you can store it in a class variable if you want a temporary storage.

In your activity's onResume() method retrieve the stored position and use

spinner.setSelection(int position);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top