4
<?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);

Jatin Sachdeva
  • 1,091
  • 11
  • 12
  • post your manifest please – David Wasser Sep 11 '17 at 13:20
  • @DavidWasser Have posted the manifest. Thanks. – Jatin Sachdeva Sep 11 '17 at 13:36
  • Please try this: install the App on the phone. Now kill the app (go to Settings->Apps->YourApp->Force close). Now launch the app from the HOME screen by clicking on its App-Icon. Now launch `ActivityB`. Now use `adb shell dumpsys activity activities` and check your task(s) in the list. Are there 1 task or 2? Post the relevant information in your question please. – David Wasser Sep 11 '17 at 14:07
  • Thanks. Have followed what you had said and posted the stack trace of activities but both are in same task. – Jatin Sachdeva Sep 12 '17 at 06:15
  • Hmmm... What device are you testing on? What version of Android on that device? What SDK are you compiling with? – David Wasser Sep 12 '17 at 08:59
  • Also please post the code you use to start `ActivityB` from `ActivityA`. – David Wasser Sep 12 '17 at 08:59
  • Device : Lenovo k50a40 Android Version : 5.0 CompileSdkVersion : 25 Code : `Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, REQUEST_CODE_NOTIFY);` – Jatin Sachdeva Sep 12 '17 at 09:41
  • 1
    please add these details in question – Amit Vaghela Sep 12 '17 at 09:58

1 Answers1

4

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?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks a lot. Yes, this was the reason. I had thought this but got confused as according to documentation, **For example : if the activity you are launching uses FLAG_ACTIVITY_NEW_TASK, it will not run in your task and thus you will immediately receive a cancel result.**. Basically I was creating an editor (MainActivity) accomplishing "singleInstance" . – Jatin Sachdeva Sep 12 '17 at 12:47
  • I still don't understand why you think you need `singleInstance`. In general you only need that if you are creating a HOME-screen replacement (which you aren't). – David Wasser Sep 12 '17 at 16:12
  • You could accept my answer if it was helpful. This will remove the question from the list of unanswered questions. Or do you still have a problem?? – David Wasser Sep 13 '17 at 15:24
  • Yes, it was very helpful. Thanks. As required, i will update this to a home-screen replacement. – Jatin Sachdeva Sep 14 '17 at 11:27