Android Spinner Saving Last Selected Option - android

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

Related

Android - How to co-ordinate Spinner and Fragments when going back?

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.

Shared Element list changes

Activity A contains a list of images. Activity B contains the selected image. Using shared elements it successfully animates the image being moved to the new activity and back again when I finish B.
The problem is when the list is changed and I am on B, the image will try to go back to the same spot in the list. If it was the 10th item in the list when I selected it, then when I press back there are only 3 images in the list now it will throw an exception as that 10th list item container won't exist anymore. If I select the 1st list image, but whilst on B the list grows and my item is forced to the 5th spot in the list, when I press back it will still go back to that 1st list item it came from.
Is there a way I can tell it to move to a different list item on its return transition? (Activity B is informed when the list changes, so I do have the option to get it's new position)
Any suggestions would be great guys.
Thanks
It seems you should only refresh your list when your ActivityA is ready.
Maybe you can use startActivityForResult() in ActivityA to start ActivityB, though I'm unsure about the exactly life-cycle callbacks of ActivityA when ActivityB returns.
Maybe you should look into SharedElementCallback and refresh your list when the transition has completed.

How can I save the state of an activity in Android

I want to save the sate of an activity even if it is destroyed, Means if i have a lot of controls on the activity view then when ever changes occur on the controls after activity is called like selected any options on the spinner or the TextView text is changed etc I can save each control state separately but I want to save the whole activity object type thing so if activity recreated then all the options on the controls are selected at once. So that there is less coding. and If I perform any animation on any control then its changed position are saved if any animation is applied on that control.
Just use Shared Preferences. Its that simple.
Refer : http://developer.android.com/reference/android/content/SharedPreferences.html
I have used Shared preferences with Gson and saved the whole object as string at once. IT is not I wanted but It helped to save the state in ore less way. Thanks for all to reply.

Persisting Actionbar Spinner state

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);

Android:listview store value of button

I have a like button in my list View Item, Whenever i click on like button it saves the values in mysql.
(cus_id, offer_id, status)
But if i go to some other activity and return back to list view activity the like button turns to be in default color.
I want to save activity done on like button, means if the like button is clicked once it should remain clicked. even after switching b\w any activity.
how can i resolve it...
You should set adapter to your list in onResume activity call back rather than onCreate Activity call back. Making this change should solve the problem.
I think you set your adapter for listview in onCreate,
You set your adapter in oncreate
Make changes to your listview item (Clicking Like Button)
Move to next activity.
Press Back and come to your list activity, your change not reflecting.
Because when you press back onCreate will not be gets called so your listview adapter refreshes its view with POJO that you have given to the adapter.
Solution
1) Set adapter in OnResume
2) I Suggest this solution, after updating SQL database, update your adapter POJO and call
adapter.notifyDataSetChanged();

Categories

Resources