Search code examples
androidandroid-intentandroid-activityandroid-manifestandroid-task

Android Activity Incorrectly Re-parenting to Application Task


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>

Solution

  • 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.