Skip to content
Related Articles

Related Articles

How to Restore Data on Configuration Changed in Android using Bundles?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 24 Jan, 2021

In Android, if the configuration of the application changes, for example when the android screen is rotated, then some data is lost and reset. Especially, the data from the variables. So this issue can be solved by overriding the functions onSaveInstanaceState() and onRestoreInstanceState(). So in this article, it’s been discussed how this issue can be resolved in detail using the flow chart, so as to understand when these above functions are called. Note that we are going to implement this project using the Kotlin language. 

Below is a flow chart represents how the methods are called and data is restored and updated in the UI:

Restore Data on Configuration Changed in Android using Bundles

Step by Step Implementation

Step 1: Create an empty activity project

Step 2: Working with the activity_main.xml file

  • The main layout of the application containing EditText, and one TextView, and two Buttons, which increment and decrement the value of the TextView.
  • To implement the UI invoke the following code inside the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
        android:hint="Enter Something"
        app:layout_constraintTop_toTopOf="parent" />
        app:layout_constraintTop_toBottomOf="@+id/editText" />
        app:layout_constraintTop_toTopOf="@+id/incrementB" />
        app:layout_constraintTop_toBottomOf="@+id/editText" />


Step 3: Working with the MainActivity.kt file

  • In the MainActivity,kt file the two functions onSaveInstanceState(outState: Bundle) and onRestoreInstanceState(savedInstanceState: Bundle) has to be overridden, the onSaveInstanceState function puts the data to bundle named outState, and onRestoreInstanceState function receives the data using Bundle named savedInstanceState. Refer to the flow chart provided above to get clear flow.
  • To implement the same invoke the following code inside the MainActivity.kt file.
  • Comments are added inside the code to understand the code in more detail.


import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    // instances of all the UI elements
    lateinit var editText: EditText
    lateinit var counterText: TextView
    lateinit var incrementB: Button
    lateinit var decrementB: Button
    // counter to increment or 
      // decrement the counter text
    var countInt: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        // register all the UI elements with 
          // their appropriate IDs
        editText = findViewById(
        incrementB = findViewById(
        decrementB = findViewById(
        counterText = findViewById(
        // handle the increment button
        incrementB.setOnClickListener {
            if (countInt >= 0) {
                counterText.text = countInt.toString()
        // handle the decrement button
        decrementB.setOnClickListener {
            if (countInt > 0) {
                counterText.text = countInt.toString()
    override fun onSaveInstanceState(outState: Bundle) {
        // put the unique key value with the data
          // to be restored after configuration changes
        outState.putInt("counterData", countInt)
    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        // get the stored data from the bundle using the unique key
        countInt = savedInstanceState.getInt("counterData")
        // update the UI
        counterText.text = countInt.toString()


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!