Lessons: 8Length: 44 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.6 Activities and Screen Rotation

In this lesson, you will learn about the lifecycle methods that are executed when the Android screen is rotated. You will see how the state of the Activity is preserved before rotation, and then how it is restored after the change.

2.6 Activities and Screen Rotation

Screen Orientation and Activity Lifecycle, this is what we will be learning in our final chapter at this course. Our devices come with the facility to change the orientation from landscape to portrait and vice versa. Now, this is the log in page, or let's say activity of the base camp project manager. This is the portrait mode, and the second is the landscape mode. When the activity is created for the first time, let's say in the portrait mode, the user enters something, and the lifecycle methods are executed like we discussed in the last lesson. When the user changes the orientation from portrait to landscape, the first activity is wiped out and then the second one is created with the landscape orientation. Now, here, the activity life cycle has a little change. We need to save all the data that we had in the previous orientation and restore it in the second orientation here from portrait to landscape. We don't want to lose the data on changing the screen orientation. These are the two lifecycle functions which we can use to preserve the state of the activity when it changes from one orientation to other. We will save the InstanceState, and then we will restore it when the new orientation activity is created. Now, what happens while restoring the activity state? Let me show you what exactly happens. Let's say this is our first activity. When this activity is created, the onCreate method is called. The we have onStart, weather activity gets focused. The we have onResume and the activity is running. The activity is now visible to the user and the user can interact with the App UI. Let's say the user entered the username in this login screen in the text field. Now, after entering the data, and while this activity is running, now let's say the user changes the orientation of the screen. Now, what happens is the first activity enters the pause state, and then it is stopped. It is no longer visible to the user. After onStop, the activity is destroyed. Now, when the activity is destroyed, the data with the user head entered, will also be destroyed. We don't want that. We want to preserve the state of the widgets and the activities that was there previously. Now, for that, before onStop and after onPause, there is this lifecycle method which is executed, onSaveInstanceState. This will preserve the state of the main activity, the values that the widgets had, the state of the widget and every other data in form of a bundle. Only after the instance is saved, the onStop method will be executed, and then the activity is destroyed. After the first activity is destroyed, onCreate method is executed for the second activity. After onCreate, we have onStart executed. Now, the activity is visible to the user, but user cannot interact. After onStart, we have onRestoreInstanceState. For onRestoreInstanceState, it restores all the data that we had on the onSaveInstanceState. It restores the state of the activity and all the widgets inside it. After restoring the instance, onResume is executed for the activity,and then the activity starts running again. So this is how the state is preserved in case of orientation change in an Android application. Let us move to Android Studio and see it ourselves. Let us override the other two methods which we had. Now, let us run our application. Open the logcat and see what exactly is going on. So here we have OnCreate, OnStart, OnResume. This is well-understood. Now, we have onCreate, onStart, onResume. This is well-understood. On changing the screen orientation, let's see what happens. So here we are. We have OnPause, then OnSaveInstanceState. The state of the activity is saved, and then we have OnStop. Then first activity is stopped, and then it is destroyed. After that, for this landscape activity, we have it created, and then OnStart method is executed, then we have onRestoreInstanceState. Now, for onRestoreInstanceState, this saved instance is the bundle which is used to reinstall the instance. After that, we have OnResume. So this is how activity lifecycle works in case of the change of orientation of your Android application. Dealing with the activity lifecycle in a proper manner is important in case you have large number of activities in your application.

Back to the top