How Android Sees Your Activities

Let’s look at what is going on OS-wise as you move between activities. First, when you click on the GeoQuiz app in the launcher, the OS does not start the application; it starts an activity in the application. More specifically, it starts the application’s launcher activity. For GeoQuiz, MainActivity is the launcher activity.

When the New Project wizard created the GeoQuiz application and MainActivity, it made MainActivity the launcher activity by default. Launcher activity status is specified in the manifest by the intent-filter element in MainActivity’s declaration (Listing 6.18).

Listing 6.18  MainActivity declared as launcher activity (manifests/AndroidManifest.xml)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ... >

    <application
        ... >
        <activity android:name=".CheatActivity">
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

After the instance of MainActivity is onscreen, the user can press the CHEAT! button. When this happens, an instance of CheatActivity is started – on top of the MainActivity. These activities exist in a stack (Figure 6.11).

Pressing the Back button in CheatActivity pops this instance off the stack, and the MainActivity resumes its position at the top, as shown in Figure 6.11.

Figure 6.11  GeoQuiz’s back stack

GeoQuiz’s back stack

A call to Activity.finish() in CheatActivity would also pop the CheatActivity off the stack.

If you run GeoQuiz and press the Back button from the MainActivity, the MainActivity will be popped off the stack and you will return to the last screen you were viewing before running GeoQuiz (Figure 6.12).

Figure 6.12  Looking at the Home screen

Looking at the Home screen

If you started GeoQuiz from the launcher application, pressing the Back button from MainActivity will return you to the launcher (Figure 6.13).

Figure 6.13  Running GeoQuiz from the launcher

Running GeoQuiz from the launcher

Pressing the Back button from the launcher will return you to the screen you were looking at before you opened the launcher.

What you are seeing here is that the ActivityManager maintains a back stack and that this back stack is not just for your application’s activities. Activities for all applications share the back stack, which is one reason the ActivityManager is involved in starting your activities and lives with the OS and not your application. The stack represents the use of the OS and device as a whole rather than the use of a single application.

(Wondering about the Up button? We will discuss how to implement and configure this button in Chapter 14.)

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

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