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.
Related
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?
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!
I'm working on an app and am using the android architecture components. I have a ViewModel that I am using to store and manage the UI data. I have an activity that displays a list of text items that are loaded from the Room database as LiveData. The views containing the LiveData are editable. Basically, it's a RecyclerView containing EditTexts.
What I'm wondering is the scenario where the user changes one of the EditText's values, and then something happens that causes the activity to restart, such as a configuration change. From what I understood, the activity is created again which means the onChanged() callback gets invoked again and the list gets repopulated with the original LiveData that came from the database. Because of that, I expected the EditText (the one whose text was edited by the user) to display the original LiveData that was assigned to it before the user changed the text. However, when I rotated my device's screen, the edited text remained.
This is what I wanted, but I didn't think this was the default behavior. I'd like to know why the edited text persisted after the configuration change since it was never saved to the database. Is this just a special property of EditTexts? This user's post describes something similar happening where their EditTexts retained their most recent states after a screen rotation while their TextViews were cleared: Restoring state of TextView after screen rotation?
I recommend reading this answer. But yes, edittexts will automatically handle state by default. Also, in your scenario the livedata didn't load the value again because the database did not change.
Found the answer to my question in this codelab: https://codelabs.developers.google.com/codelabs/android-lifecycles/#6
Some UI elements, including EditText, save their state using their own onSaveInstanceState implementation. This state is restored after a process is killed the same way it's restored after a configuration change.
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 am developing twitter client.
I have TimelineFragment with loaders which load data from db and web.
I have setRetainInstance(true) in onActivityCreated.
When orientation change view recycled? how i can prevent this?
It depends on what you are trying to save. Your member variables will be saved but you will have to use them to reinitialize your view widgets such as TextView, EditText, Buttons.
Alternatively if its simple data you want to save such as text on TextViews, EditText, Buttons, etc.., you can use an xml feature on them called "freezesText"