recyclerview save state in Activity - android

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?

Related

OnScreenRotation with RecyclerView in tabbed Activity

I am using a tabbed Activity. In one of the tabs I have two buttons and a Recyclerview. In the RecyclerView I have two spinners and an edittext.
The purpose of the first button is to add a new item in the RecyclerView and the second one is to save the chosen elements in the spinners and edittexts.
My problem is that when the screen is rotated, the Recyclerview is emptied. I tried using the OnSaveInstanceState() and the OnRestoreInstanceState of the LayoutManager of the Recyclerview to save the state but it still gets deleted.
I'd appreciate any help !
By default, activities and fragments have an onSaveInstanceState() method that the system uses to provide a Bundle to which you can write primitive data and parcelable objects.
This is okay as long as your data is simple. In your case, it isn't.
The framework may decide to destroy or re-create a UI controller in response to certain user actions or device events that are completely out of your control.
For example screen rotation (orientation change).
ViewModel comes to the rescue.
ViewModel is a class that’s part of the Android Architecture Components and it is lifecycle aware.
For more check this documentation.

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.

Save state of specific toggle button in ListView

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.

Android: Preserve complex view data of Fragments

In my application I have a FragmentActivity that uses a FragmentViewPager. The Fragments have a quite complex structure of views with ListViews and Adapters. The FragmentViewPager appears to destroy the Fragments that are out of sight, and to recreate them if the user swipes back to it. This leads to all member variables are cleared that hold the lists and adapters with all content, so the Fragment is empty if it comes back to the top.
My question now is: How can I preserve these data? If I'm not mistaken, I have to save and resume the list with the content the adapter works on. When and where should I save it?
Is it a good idea to save the data in the FragmentActivity or in a helper singleton class outside the activity?
How can I preserve these data?
Your data is already "preserved". You loaded it from somewhere originally from some data model. Load it again.
If there are changes the user is making on-screen to the data, you need to be persisting that to your data model as the user makes the changes. This is no different than dealing with, say, a RatingBar in a ListView row.

Categories

Resources