2

I got 2 activities, say A and B, A is started as usual through the launcher icon. B on the other hand is started through BroadcastReceiver. The thing I don't really understand here is when activity B is destroyed then activity A is opened? I just can't see the connection in this, and this behavior is not wanted. I've solved this problem before by disabling history on activity A but new functionality enforces history to be used so another solution is needed for this.

Activity declaration in manifest:

<activity android:name="com.example.A" 
    android:noHistory="false" android:launchMode="singleTask"  
    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="com.example.B" android:noHistory="true" 
    android:screenOrientation="portrait" android:label="@string/APP_NAME">
</activity>

And how activity B is launched from BroadcastReceiver:

Intent intent = new Intent(context, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(intent);

I just don't really get how activity B finds it way to activity A after B is destroyed? How could this be prevented?

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
Robert
  • 4,602
  • 4
  • 22
  • 33
  • It's [activity's tasks and back stack](http://developer.android.com/guide/components/tasks-and-back-stack.html) that causes this. when activity B is launched, activity A will be stored in the back stack, when activity B is destroyed, activity A will be brought to foreground. – Weiyi Apr 06 '15 at 02:22
  • @li2, Okey, I understand that activity A must be stored in the backstack for this behaviour to occur, the thing I dont yet understand is how activity A is placed in the backstack? Since activity B is started from a receiver, which has nothing to to with activity A? – Robert Apr 06 '15 at 09:27

1 Answers1

1

When you launch B, Android checks if there is already an existing task with the same taskAffinity as B (in your case, the task associated with A). If it finds one, it brings that task to the foreground and launches B on top of it. When B is finished, it drops the user into A.

To change this behaviour, you need to give B a different taskAffinity than A. Add this to the manifest declaration for B:

android:taskAffinity=""

Now B will be launched in its own task, even if there is already an existing task with A in it.

Be aware, however, that if A and B are running in separate tasks, you should probably provide a different icon, or at least a different label, for B, otherwise the user will be confused when he looks in the recent tasks list. He will see one entry for A and one entry for B, but they will both have the same icon and label.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • @David Wasser, can you answer my question, please? https://stackoverflow.com/questions/46427805/why-behaviours-are-different-androidlaunchmode-singletask-androidtaskaf – zephyr Sep 27 '17 at 05:54