I've been learning Android and have come across an issue with
launchMode="singleTask". The documentation states that when this
attribute is used, the Activity is always launched into a new task as
the root Activity. Secondly, the documentation states that if an
Intent is targeted at such an Activity when there are Activities
sitting above it in its task stack, such Intents are discarded
(although the task is still brought to the foreground).

I've been playing around with this, and the behaviour I observe is
completely different. In particular: - Activities with
launchMode="singleTask" are not always the root Activity in a task
stack. They are just plonked ontop of the existing stack with the same
affinity. - When an Intent is targeted at such an Activity and there
are other Activities above it in the stack, the Intent is not
discarded. Instead the Activities above it in the stack are discarded.
The Intent is then delivered via onNewIntent to the Activity as
normal.

Can someone confirm that this is the actual behaviour? If so, why are
the documents incorrect? If not what have I done wrong. . . Here is a
simple way to observe the behaviour:

1. Create a simple main.xml containing two buttons b1 and b2.
2. Create the following Activity:

public class ActivityA extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button lButton = (Button) findViewById(R.id.b1);
        lButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                                Intent lNextIntent = new 
Intent(ActivityA.this,ActivityA.class);
                                startActivity(lNextIntent);
                        }
        });

        Button lButton2 = (Button) findViewById(R.id.b2);
        lButton2.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                                Intent lNextIntent = new 
Intent(ActivityA.this,ActivityB.class);
                                startActivity(lNextIntent);
                        }
        });
    }
}

3. Create the second Activity:

public class ActivityB extends ActivityA {

}

4. The manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="ojw28.activitytest"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <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:launchMode="singleTask">
        </activity>
    </application>
</manifest>

5. Launch the application. Press button b1 a couple of times, see what
happens to the task stack using "adb shell dumpsys activity". Multiple
instances of ActivityA are now in the task stack as expected.

Now press button b2. According to the documentation this should launch
an ActivityB instance is a NEW task, as the root Activity of that
task. Use adb to look at the stack. What actually happens is ActivityB
gets put ontop of the current stack, which isn't the documented
behaviour.

Now press button b1 a couple more times. Then press b2 again. Since
the single instance of ActivityB isn't at the top of the stack, the
documentation states that this intent should be ignored. The observed
behaviour is that all ActivityA instances above the ActivityB instance
are discarded from the stack, after which the intent is delivered to
the instance of ActivityB as normal (via onNewIntent - you can
override this method and add a break point to observe this).

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to