How to Save UI States | Android Activity State onSaveInstanceState Method – Kotlin

Saving a UI state in the android app is very important, UI state to remain the same, but the system destroys the activity. To improve user experience and handle it system behavior you can use ViewModel object, onSaveInstanceState() method and local storage to persist the UI state across such application.

You can use any or combination of method depend on the complexity of your UI data, use cases for your app. And you have to sure about the speed of the app should doesn’t slow.

Save UI State Android Activity State onSaveInstanceState Method

 Note:- onRestoreInstanceState() is called after onResume()

Let’s Build an app Android Activity State UI app with onSaveInstanceState :

In this example, we will see how to save state when the android app rotates. In the rotation, activity is again created.

If the user complete dismissal case means have permanently navigated away from the activity, so re-open activity will start from a new state.

User-initiated UI state dismissal

  • Pressed back button
  • Swiping recent app
  • Navigating up from the activity
  • killing the app from the Settings screen
  • finishing” activity – Activity.finish()

Step 1. Create a new project “Build Your First Android App in Kotlin

Step 2. Add following code in “activity_main.xml

A UI has 2 EditText and a checkbox.

Step 3. Open the “MainActivity.kt” and add the following code

Step 4. Now Run the application, in an emulator or on your Android device

Output screenshot Android Save UI States example :

A second edit box doesn’t have saving state code, so when the screen has rotated all data of it gone.

Android on Save Instance State

Q: What is the use of saved-Instance-state in android studio?

Answer: onSaveInstanceState() and other such lifecycle callbacks give you an opportunity to save any application state that you want to restore when the user returns to your application.

For example, let’s say you have a simple text field in your Android App. Consider this workflow:

  1. The user opens your app
  2. The user enters some information into your text field
  3. The user gets a notification saying they have a new email, and clicks on that notification to read the email
  4. The user returns to your application

In this case, you likely want to keep the user’s input around so they don’t have to type it in again. This is a great use of the component’s saved instance state.

Q: What are the Options for preserving UI state?

Answer: To save a UI state in android has multiple options, like:-

  • ViewModel – in memory,
  • Saved instance state – serialized to disk,
  • Persistent storage- on disk or network

Download source code onSaveInstanceState in kotlin

https://github.com/EyeHunts/AndroidSaveInstanceState

Sources: https://developer.android.com/topic/libraries/architecture/saving-states

Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=”25″
TargetSdkVersion=”28″
Coding in Kotlin


3 thoughts on “How to Save UI States | Android Activity State onSaveInstanceState Method – Kotlin

  1. My bad, I learn English by my own. User have to write in textfield but when close or change activity words disappear. I would like keep this words on screen even after close or change activity to show when user go back. Really thank you by reply!

  2. Hi! thanks a lot to teach! I´m trying to keep data between activities. User put on a textfield a word and go to other activity and when return the textfield it´s still there. Could you help me with that? one week looking for the answer and no solution… thank you again for your tutorials!

    1. Your question is not clear, do you want clear textfiled? or want remove activity? when the user returns the previous activity. finish activity when going another to user not get back the same activity once fill it.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.