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 its 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 methods depending 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.
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 completes the dismissal case means have permanently navigated away from the activity, so the 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.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/f_name_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="First Name"
android:layout_margin="10dp"/>
<EditText
android:id="@+id/l_name_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Last Name"
android:layout_margin="10dp"/>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox"
android:layout_margin="10dp"/>
</LinearLayout>
Step 3. Open the “MainActivity.kt” and add the following code
package com.eyehunts.androidsaveinstancestate import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import kotlinx.android.synthetic.main.activity_main.* import kotlin.math.log class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } public override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) savedInstanceState.putString("f_name", f_name_et.text.toString()) //savedInstanceState.putString("l_name", l_name_et.text.toString()) savedInstanceState.putBoolean("cb_value", checkBox.isChecked) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) //get value val fn = savedInstanceState.getString("f_name") //val ln = savedInstanceState.getString("l_name") val cb = savedInstanceState.getBoolean("cb_value") //set value to UI f_name_et.setText(fn) //l_name_et.setText(ln) checkBox.isChecked = cb } }
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.
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:
- The user opens your app
- The user enters some information into your text field
- The user gets a notification saying they have a new email, and clicks on that notification to read the email
- 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
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!
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!
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.