Mark
Mark

Reputation: 39881

Not understanding Activity stack?

I'm having trouble with my activities when they go in the background. I have two activities, A and B. Only A can launch B (manifest copied below). This is what I do:

  1. Launch the app from the app drawer (activity A)
  2. There's a button on A to launch B, B now is displayed.
  3. Hit the Home key, my app is now in the background.
  4. Go to the app drawer again, hit my app icon.
  5. Instead of resuming at ActivityB, Activity A is launched again. Why?
  6. If I hit the Back key now, I get shown the old ActivityB. What?

This is very confusing. It's like Android knows my app is running, and puts a new instance of A on top of the old B instance running. I'd just expect that the application gets paused in-place, and whenever the user hits the app icon again, it just picks up where it left off (in this case, just show B again!) Below is the manifest, and the activity classes for this test are completely empty (except A which has a button to launch B).

<activity android:name=".ActivityA"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".ActivityB"
          android:label="@string/app_name">
        <intent-filter>
        <!-- 
            <action android:name="android.intent.action.VIEW" />
             -->
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Upvotes: 1

Views: 1061

Answers (3)

Savvas Dalkitsis
Savvas Dalkitsis

Reputation: 11592

Have you set the following property in your activity's xml definition?

android:launchMode="singleTop"

Upvotes: 0

Mark
Mark

Reputation: 39881

I'm experiencing this bug:

http://code.google.com/p/android/issues/detail?id=2373#makechanges

Thanks for your help, this has been so confusing.

Upvotes: 1

Anthony Forloney
Anthony Forloney

Reputation: 91786

If I had to take a guess because inside your app drawer is your .apk reference for your Program A. Regardless if B is already launched, the INTENT of the application when selected is to launch Program A.

I have only read a little on Android, so I can not say with 100% certainty, but that is my guess.

I hope this helps.

EDIT:

Holding down the Home key on the Android phone, shows the list of background apps, can you do the same inside of the emulator and see if Program B pops up? Or same problem occurs?

After reading things around Google, I came across this here is a little snippet:

All the activities in a task move together as a unit. The entire task (the entire activity stack) can be brought to the foreground or sent to the background. Suppose, for instance, that the current task has four activities in its stack — three under the current activity. The user presses the HOME key, goes to the application launcher, and selects a new application (actually, a new task). The current task goes into the background and the root activity for the new task is displayed. Then, after a short period, the user goes back to the home screen and again selects the previous application (the previous task). That task, with all four activities in the stack, comes forward. When the user presses the BACK key, the screen does not display the activity the user just left (the root activity of the previous task). Rather, the activity on the top of the stack is removed and the previous activity in the same task is displayed.

I hope this points you in the right direction

Upvotes: 2

Related Questions