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.
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);
Related
I have a Spinner in my activity which works very simply: When the user selects an item in the spinner, the appropriate Fragment is shown in the activity by replacing the previously-shown fragment.
My spinner uses a SimpleCursorAdapter. And each fragment transaction is added to the back stack.
Everything works fine when selecting spinner items, but when I press the device's BACK button, the spinner selection is not updated.
I see FragmentManager has an addOnBackStackChangedListener() method, but I'm not sure if that is the best way to go (or if it could even work at all)...
So is there a convenient (or other) way to co-ordinate the spinner's selection with the current fragment when going back?
NB - I suppose what I'm ideally looking for would be some kind of Spinner/Fragment equivalent of the TabLayout/ViewPager's tabLayout.setupWithViewPager(viewPager) method.
Hey I was wondering how I can save the state of a specific togglebutton's state in a ListView. So that whenever my activity is started, those togglebuttons would stay checked.
Any help is appriciated.
Thankyou
There are several options for saving data beyond the life cycle of your activity and application. The Storage Guide has a rundown of each, along with their merits. Depending on what your toggle button and list view represent, you may just need to use a Preference Activity, which simplifies the saving and recalling of any settings you've built in to your app.
You can manually save a boolean in your Shared Preferences and recall it when creating the adapter for your list view. There's nothing about the toggle button itself that should be responsible for persisting this state data - it should only be in charge of displaying the state as you tell it.
I have a relativelayout with 30 buttons within a scrollview. What I would like to know is if there is a way of saving the position, ie. when the user has scrolled down to the second last button and clicked on it to display an image or text, and when pressed back to select a different button to return to the last position (which was the second last button for example) without having to scroll all the way down again? I had a sliding drawer and it worked fine, but with the buttons (which are image buttons) it takes a while to load the screen with all the buttons. And I found doing it without the sliding drawer it loads faster but now have to scroll all the way down to the button every time I return to the buttons.
Android docs say here:
[...] you should use the onPause() method to write any
persistent data (such as user edits) to storage. In addition, the
method onSaveInstanceState(Bundle) is called before placing the
activity in such a background state, allowing you to save away any
dynamic instance state in your activity into the given Bundle, to be
later received in onCreate(Bundle) if the activity needs to be
re-created.
Perhaps you just recreating your activity content by accident.
I have a Spinner in my Activity with a list of Options. By default my first Item on the list is in Selected State when I start the Activity.
Assume My List has <Circle> <Square> <Rectangle> <Triangle>
When Activity is created for the first time Circle is Selected. Suppose I select Rectangle. Then I navigate to another Activity and then recreate the Activity. My selection is restored to the first Item again. How can I save my previous selected option.
What are the Object types in your list? Strings?
Try overriding onSaveInstanceState and onRestoreInstanceState
This answer has a nice example: Saving Android Activity state using Save Instance State
I would like to write a rather simple content application which displays a list of textual items (along with a small pic).
I have a standard menu in which each menu item represents a different category of textual items (news, sports, leisure etc.). Pressing a menu item will display a list of textual items of this category.
Now, having a separate ListActivity for each category seems like an overkill (or does it?..)
Naturally, it makes much more sense to use one ListActivity and replace the data of its adapter when each category is loaded.
My concern is when "back" is pressed. The adapter is loaded with items of the current category and now I need to display list of the previous category (and enable clicking on list items too...).
Since I have only one activity - I thought of backup and load mechanism in onPause() and onResume() functions as well as making some distinction whether these function are invoked as a result of a "new" event (menu item selected) or by a "back" press.
This seems very cumbersome for such a trivial usage... Am I missing something here?
Thanks, Rob
If the user hits the back button your Activity will likely get garbage collected.
If you properly start your activity from the menu with the different categories through an Intent, with passing the category etc. and then choosing the content in the onCreate method, you will get a new Instance of your Activity every time the user chooses a category, that will be destroyed after the user hits the back button.
This behaviour is perfectly fine. You don't have to cope with strange error cases and populating the list will take some time so the object creation time of the new ListActivity will be no problem. Try to program it as easy as possible for you and then test if there is a performance problem in the end of doing so.