views:

548

answers:

5

My main activity A has as set android:launchMode="singleTask" in the manifest. Now, whenever I start another activity from there, e.g. B and press the HOME BUTTON on the phone to return to the home screen and then again go back to my app, either via pressing the app's button or pressing the HOME BUTTONlong to show my most recent apps it doesn't preserve my activity stack and returns straight to A instead of the expected activity B.

Here the two behaviors:

Expected: A > B > HOME > B
Actual: A > B > HOME > A (bad!)

Is there a setting I'm missing or is this a bug? If the latter, is there a workaround for this until the bug is fixed?

FYI: This question has already been discussed here. However, it doesn't seem that there is any real solution to this, yet.

A: 

If both A and B belong to the same Application, try removing

android:launchMode="singleTask"

from your Activities and test because I think the default behavior is what you described as expected.

dtmilano
If I remove android:launchMode="singleTask" it creates a new instance of A (my main activity) whenever I call an Intent to that, e.g. I have a menu shortcut to go back to my main activity (my applications home screen).
znq
For going back to the main home screen I use an Intent with FLAG_ACTIVITY_CLEAR_TOP. This way, you get back to the first instance of your main Activity and remove all the activities that were on top.
kaciula
A: 

Stefan, you ever find an answer to this? I put together a testcase for this and am seeing the same (perplexing) behavior...I'll paste the code below in case anyone comes along and sees something obvious:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example" >

  <uses-sdk android:minSdkVersion="3"/>

  <application android:icon="@drawable/icon" android:label="testSingleTask">

    <activity android:name=".ActivityA"
              android:launchMode="singleTask">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>

    <activity android:name=".ActivityB"/>

  </application>
</manifest>

ActivityA.java:

public class ActivityA extends Activity implements View.OnClickListener
{
  @Override
  public void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.main );
    View button = findViewById( R.id.tacos );
    button.setOnClickListener( this );
  }

  public void onClick( View view )
  {
    //Intent i = new Intent( this, ActivityB.class );
    Intent i = new Intent();
    i.setComponent( new ComponentName( this, ActivityB.class ) );
    startActivity( i );
  }
}

ActivityB.java:

public class ActivityB extends Activity
{
  @Override
  public void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.layout_b );
  }
}

I tried changing minSdkVersion to no avail. This just seems to be a bug, at least according to the documentation, which states the following:

As noted above, there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents. A "singleInstance" activity is always at the top of the stack (since it is the only activity in the task), so it is always in position to handle the intent. However, a "singleTask" activity may or may not have other activities above it in the stack. If it does, it is not in position to handle the intent, and the intent is dropped. (Even though the intent is dropped, its arrival would have caused the task to come to the foreground, where it would remain.)

Eric
+1  A: 

This is not a bug. When an existed "singleTask" activity is launching, all other activities above it in the stack will be destroyed.

When you press HOME and launch the activity again, ActivityManger calls an intent{act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flat=FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_IF_NEEDED cmp=A}, so the result is A > B > HOME > A.

It's different when A's launchMode is "Standard". The task which contains A will come to the foreground and keep the state the same as before.

You can create a "Standard" activity eg. C as the launcher and startActivity(A) in the onCreate method of C

OR

Just remove the launchMode="singleTask" and set FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP flag whenever call an intent to A

renchenyu
A: 

Hi Stefan, Whenever you press the home button to go back to your home screen the activity stack kills some of the previously launched and running apps.

To verify this fact try to launch an app from the notification panel after going from A to B in your app and come back using the back button ..........you will find your app in the same state as you left it.

Siddhartho