Understanding Activity Lifecycle to Retain UI Data when Back Pressed in Android
What is onBackPressed() in Android?
This is an override function called when the user presses the back button on an Android device. It has great implications on the activity lifecycle of the application. The official documentation states that onBackPressed() method is called when the activity has detected the user’s press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
So, through this article, we will show you how you can override this method and retain the activity data. But before that, it is necessary to understand the Activity Lifecycle in Android. Refer to this article for a better understanding of the Activity Lifecycle in Android: Activity Lifecycle in Android with Demo App.
This article has 4 parts:
- Create a template code to understand the navigation key events and linked Activity Lifecycle methods (Skip if you know Activity Lifecycle)
- Override the on back pressed method
- Testing EditText
- Testing TextView
1. Create a template code to understand the navigation key events and linked Activity Lifecycle methods
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Override all the methods in Activity Lifecycle
We implemented a Toast message in every method to understand which method is called when during the Activity Lifecycle. Now simply run the project on a device or an emulator.
Step 3: Run the program
- When the application starts: onCreate, onStart, onResume
- When App Overview Button is pressed: onPause, onStop
- When Home Button is pressed: onPause, onStop
- When Back Button is pressed: onPause, onStop, onDestroy
We can see that in any case other than the back press, the activity is stopped. Basically, it is not destroyed and data is retained. However, when the back button is pressed, the activity is destroyed, meaning, the temporary data is lost during this call. This is what the official documentation states.
But we do not want to lose this data. To retain the data, we need to override the back pressed method. Back pressed method by nature destroys the activity. We will make changes in such a way that the activity will stop but not destroy. Continue reading.
2. Override the on back pressed method
Please read the comments in the code.
Now we can observe that on the back press, the activity will stop but not destroy. We shall now implement some UI for testing if this actually works. Keep reading.
3. Testing EditText
Step 1: Add EditText in the layout file (activity_main.xml)
For testing the EditText, we need not code anything in the main code other than what is specified in the previous (Override the on-back pressed method) code.
Type anything in the EditText and press the back button. Return back to the app and you will notice that text is retained in the EditText bar.
The text in the EditText is retained. This means our method works fine. We shall now test it on a TextView. Keep reading.
4. Testing TextView
Step 1: Add a TextView and a Button in the layout file (activity_main.xml)
Step 2: Add functionality to these elements via the main code (MainActivity.kt)
Keep clicking the button, the number in TextView will keep incrementing. Now try the back button and open the app again. The text is retained in the TextView.
Our method works perfectly fine. Now use this method in your codes to retain the activity data.