tags:

views:

304

answers:

3

Hi,

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>
+1  A: 

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

Anthony Forloney
After several days of trying to figure this out, it seems like this is what's happening:Eclipse, the marketplace, and the browser, all launch your app after install using this flag: Intent { flags=0x10000000 if you hit the home button, and resume the app via the app tray, android will put a new instance of your app on top of the already-running instance launched by Eclipse/Marketplace/Browser.There has got to be a way around this, it's so confusing for the user. This is happening with all apps I am downloading.
Mark
+1  A: 

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.

Mark
A: 

Check how I workarounded this - http://github.com/cleverua/android_startup_activity

Arhimed