Save state of specific toggle button in ListView - android

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.

Related

recyclerview save state in Activity

What would be the best way to save state without sharedpreferences? In my simple app, It is possible to create several recyclerview, but everytime I change the activity and return or rotate screen, they are all gone. I am doing both recycling view and the life-cycle of activity, but it is pretty difficult to understand for me.
Again, the point is What would be the best way to save state without sharedpreferences?

Fragment view state not saved on back press when using Android Jetpack Navigation

In my app I'm using the Jetpack navigation component and I have an issue with fragment state not being saved when navigating back to a fragment.
When I navigate from MainFragment to SettingsFragment it's via the overflow menu:
NavigationUI.onNavDestinationSelected(item, Navigation.findNavController(view));
When navigating back to MainFragment, the visibility on some views and text in some TextViews is not saved, and my state machine variable has lost its state as well.
I've read solutions where the root view is saved in a global variable in the fragment, and while this solves the visibility issue on views, the TextViews are still empty and the state machine variable is reset.
Is there a proper way to make sure fragment state is saved in this case?
If you're using view model then it can save state for you. However, that only works for simple views. For complex views including some custom views that you created, make sure that you have assigned a unique id to those as Android uses those ids to recover their state. You can use generateViewId() method on View to do so. Worst case, you might need to implement onSavedInstanceState and onRestoreInstanceState on your views.
Also, make sure you have not set to setRetainInstance to false in the xml or code.
While doing that please make sure you use parcelize annotation for your parcelable data models as this can save you a lot of time.
I hope your problem is solved by assigning unique IDs and you don't have to deal with saving state. Good luck!

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

How to navigate between views? without losing data of the initial view

I have a login page which has a Advanced button.
The button sets a view where a user can add some information.
When I click save, it goes back to my main view (loginpage) and all info is cleared?
How can I save the state of the first view when navigating away and then back to it?
Thanks
Take a look at the activity lifecycle, described in the activity docs. When an activity goes into the background, as when you start a new activity, it can be deleted and will be recreated when the user goes back to the activity.
Additionally, your view will be recreated when you go through a configuration change, like a change in orientation.
In your case, you probably want to save the state in the onPause() method, and reset it in the onResume() method. One easy way to share state between activities, is via SharedPreferences. See Saving Persistent State for an example.

Categories

Resources