Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Android SavedState is used to save information regarding the UI’s state of your application. So when users will always come back to the application then find it in the same state they had left it in.
A process is created for every launched application on android for running. The application process is killed when the application is no longer in the foreground.
Use android SavedState in this case to allow users to present the application in the same state they left it in, even if its process may have been killed by the system.
For retaining a small amount of UI related data use onSaveInstanceState() in the following situations:
Call onSaveInstanceState() in situations when activity stops, but not finished, by the system. It is not in the case when the user explicitly closes the activity or in other cases, of finish() is call.
For retaining large and complex UI-related data use ViewModel which makes the code more modular by separating the data from UI code.
ViewModels always help to handle the situation of configuration changes because ViewModel is not destroying data on a configuration change.
But for the process stop situation ViewModel destroy as well so we need to restore UI data using onSaveInstance().
Add below dependency in build.gradle in your app module.
Finally the ViewModel class will look like this
In your activity or fragment’s onCreate, update your call to ViewModelProvider to:
In this blog, you have learned to persist data for UI state. Using this approach everywhere is not a good idea and is not a replacement over SharedPreferences sometimes a Room Database or SharedPreferences might work better.
Thanks for reading this blog. You can also check other blogs from here.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
The results provided were above our expectations both in deliverability timeline as well as operation. They are a top-notch company with many resources and a company we feel honoured to work with again!
Lee Seward
Founder, Gro-Connect
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.