Single Instance : Launch Mode of Launcher Activity

1.7k views Asked by At
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jatin.notification">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:launchMode="singleInstance" > <!-- Activity A -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NotificationActivity"
            >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".DialogActivity"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:theme="@style/Theme.AppCompat.Dialog.MinWidth" />
        <activity
            android:name=".SecondActivity" /><!-- Activity B -->
    </application>

</manifest>

According to Single Instance,the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

But, when i navigated from Activity A(Launcher Activity)-> Activity B via startActivity(intent*) instead of being in new Task Activity B gets on top of Activity A's task. Though when i navigated to A from B via startActivity(intent*) it shows single instance of A.

*NO FLAGS WERE ADDED.

Why did Activity B pushed on top of Activity A(as Activity had the launch mode : "singleInstance") instead of creating a new task?

List of activities :

TaskRecord{14ba4a25 #18 A=com.example.nischay.notification U=0 sz=2} Run #1: ActivityRecord{2a37b313 u0 com.example.nischay.notification/.SecondActivity t18} Run #0: ActivityRecord{1ab16fa7 u0 com.example.nischay.notification/.MainActivity t18}

mResumedActivity: ActivityRecord{2a37b313 u0 com.example.nischay.notification/.SecondActivity t18} mLastPausedActivity: ActivityRecord{1ab16fa7 u0 com.example.nischay.notification/.MainActivity t18}

Details :

Device : Lenovo k50a40 Android Version : 5.0 CompileSdkVersion : 25

Code

Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, REQUEST_CODE_NOTIFY);

1

There are 1 answers

4
David Wasser On BEST ANSWER

Bingo! Finally an explanation for this strange behaviour!

You said you start SecondActivity from MainActivity like this:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE_NOTIFY);

When using startActivityForResult(), the Activity that is launched must run in the same task as the Activity that expects the result (ie: the launching Activity). Because of that, Android is ignoring the launchMode of MainActivity and starting SecondActivity in the same task.

You have created a conflict that isn't documented. To solve your problem you need to decide what you want. You cannot have a singleInstance Activity that calls startActivityForResult(). Either choose another mechanism to communicate between SecondActivity and MainActivity or remove the special launch mode for MainActivity.

Why do you want MainActivity to be singleInstance anyway? Is there a reason for this?