1

I have an application which has the usual task behaviour for most activities, but it also has one activity that needs to be launched onto the task that created it.

This activity is launched from a mime type. If another application launches the activity, the activity should show within the same task. However the activity is always re-parented to the task of its application. This is despite explicitly setting allowTaskReparenting to false.

When there are no other activities in the application, the activity correctly launches in the task where it was launched. It only re-parents when other activities of the application exist within a task.

How can I get the activity to only launch on-top of the activity where it was launched, separate from the other activities of the same app? Here is the relevant part of the AndroidManifest.xml:

<activity
    android:name="com.matthewmitchell.peercoin_android_wallet.ui.RestoreWalletActivity"
    android:configChanges="orientation|keyboard|keyboardHidden"
    android:theme="@style/My.Theme.Dialog"
    android:allowTaskReparenting="false" >
    <intent-filter android:label="@string/import_keys_intent_filter_action" >
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="application/x-peercoin-wallet-backup" />
        <data android:mimeType="application/octet-stream" />
    </intent-filter>
</activity>
Matthew Mitchell
  • 5,293
  • 14
  • 70
  • 122

1 Answers1

6

You must set android:taskAffinity="" in the <activity> tag in the manifest. If you don't, the Activity will have the default taskAffinity, which is the same as all the other activities in your application. Setting the taskAffinity to an empty string tells Android that this Activity doesn't prefer to be in any specific task.

This is not well-documented, but taskAffinity trumps most other settings. Android will always try to find a task with the same taskAffinity to launch an Activity into.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thank you. You are correct. Setting the affinity to the name of the activity works well. The docs do say that the activity is added to the task with the specified affinity when using `FLAG_ACTIVITY_NEW_TASK`. Funnily I'm now thinking that I want the activity to start in the main application task anyway, but I'm sure this will help some other people. – Matthew Mitchell Aug 19 '15 at 19:58
  • if android:taskAffinity="" is set and Activity is launched with FLAG_ACTIVITY_NEW_TASK. then will new activity be opened in new task or same task? As per android doc such activities don't have any affinity for any task. – Vivek Mangal Jun 25 '20 at 15:18