Exploring How the Activity Lifecycle Responds to User Actions

Three lifecycle functions were called after GeoQuiz was launched and the initial instance of MainActivity was created: onCreate(Bundle?), onStart(), and onResume() (Figure 3.7). Your MainActivity instance is now in the resumed state (in memory, visible, and active in the foreground).

As you continue through the book, you will override the different activity lifecycle functions to do real things for your application. When you do, you will learn more about the uses of each function. For now, have some fun familiarizing yourself with how the lifecycle behaves in common usage scenarios by interacting with your app and checking out the logs in Logcat.

Temporarily leaving an activity

If GeoQuiz is not already running on your device, run the app from Android Studio. Now press the Home button. The home screen displays, and MainActivity moves completely out of view. What state is MainActivity in now? Check Logcat for a hint. Your activity received calls to onPause() and onStop(), but not onDestroy() (Figure 3.8).

Figure 3.8  Pressing the Home button stops the activity

Pressing the Home button stops the activity

By pressing the Home button, the user is telling Android, I’m going to go look at something else, but I might come back. I’m not really done with this screen yet. Android pauses and ultimately stops the activity.

So after you press the Home button from GeoQuiz, your instance of MainActivity hangs out in the stopped state (in memory, not visible, and not active in the foreground). Android does this so it can quickly and easily restart MainActivity where you left off when you come back to GeoQuiz later.

(This is not the whole story about pressing the Home button. Stopped applications can be destroyed at the discretion of the OS. See Chapter 4 for the rest of the story.)

Go back to GeoQuiz by selecting the GeoQuiz task card from the overview screen. To do this, press the Recents button, which is next to the Home button (Figure 3.9).

Figure 3.9  Back, Home, and Recents buttons

Back, Home, and Recents buttons

If your device does not have a Recents button, and instead has a single Home button (as shown in Figure 3.10), swipe up from the bottom of the screen to open the overview screen. If neither of these approaches works on your device, consult the device manufacturer’s user guide.

Figure 3.10  Single Home button

Single Home button

Each card in the overview screen represents an app the user has interacted with in the past (Figure 3.11). (By the way, the overview screen is often called the “Recents screen” or “task manager” by users. We defer to the developer documentation, which calls it the “overview screen.”)

Figure 3.11  Overview screen

Overview screen

Click on the GeoQuiz task card in the overview screen. MainActivity will fill the screen.

A quick look at Logcat shows that your activity got calls to onStart() and onResume(). Note that onCreate(…) was not called. This is because MainActivity was in the stopped state after the user pressed the Home button. Because the activity instance was still in memory, it did not need to be created. Instead, the activity only had to be started (moved to the paused/visible state) and then resumed (moved to the resumed/foreground state).

Earlier, we said that it is possible for an activity to hang out in the paused state, either partially visible (such as when a new activity with either a transparent background or a smaller-than-screen size is launched on top) or fully visible (in multi-window mode). Let’s see multi-window mode in action.

Multi-window mode is only available on Android 7.0 Nougat and higher, so use an emulator to test it if your device is running an earlier version of Android. Open the overview screen again and long-press the icon at the top of the GeoQuiz card. Select Split screen (shown on the left in Figure 3.12), and a new window appears on the bottom portion of the screen displaying a list of app cards (shown in the center in Figure 3.12). Click on one of the cards to launch the corresponding app.

This launches multi-window mode with GeoQuiz in the top window and the second app you selected in the bottom window (shown on the right in Figure 3.12).

Figure 3.12  Opening two apps in multi-window mode

Opening two apps in multi-window mode

Now, click on the app in the bottom window and look at the logs in Logcat. onPause() was called on MainActivity, so the activity is now in the paused state. Click on GeoQuiz in the top window. onResume() was called on MainActivity, so the activity is now in the resumed state.

To exit multi-window mode, swipe the window separator in the middle of the screen down to the bottom of the screen to dismiss the bottom window. (Swiping the separator up dismisses the top window.)

Finishing an activity

Press the Back button on the device and then check Logcat. Your activity received calls to onPause(), onStop(), and onDestroy() (Figure 3.13). Your MainActivity instance is now in the nonexistent state (not in memory and thus not visible – and certainly not active in the foreground).

Figure 3.13  Pressing the Back button destroys the activity

Pressing the Back button destroys the activity

When you pressed the Back button, you as the user of the app finished the activity. In other words, you told Android, I’m done with this activity, and I won’t need it anymore. Android then destroyed your activity’s view and removed all traces of the activity from memory. This is Android’s way of being frugal with your device’s limited resources.

Another way the user can finish an activity is to swipe the app’s card from the overview screen. As a developer, you can programmatically finish an activity by calling Activity.finish().

Rotating an activity

Now it is time to get back to the bug you found at the beginning of this chapter. Run GeoQuiz, press the NEXT button to reveal the second question, and then rotate the device. (On the emulator, click the rotation icon in the toolbar.)

After rotating, GeoQuiz will display the first question again. Check Logcat to see what has happened. Your output should look like Figure 3.14.

Figure 3.14  MainActivity is dead. Long live MainActivity!

MainActivity is dead. Long live MainActivity!

When you rotated the device, the instance of MainActivity that you were looking at was destroyed, and a new one was created. Rotate the device again to witness another round of destruction and rebirth.

This is the source of your GeoQuiz bug. Each time you rotate the device, the current MainActivity instance is completely destroyed. The value that was stored in currentIndex in that instance is wiped from memory. This means that when you rotate, GeoQuiz forgets which question you were looking at. As rotation finishes, Android creates a new instance of MainActivity from scratch. currentIndex is initialized to 0 in onCreate(Bundle?), and the user starts over at the first question.

You will fix this bug in Chapter 4. Before making the fix, take a closer look at why the OS destroys your activity when the user rotates the device.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.91.19.28