androidandroid-notificationsback-stackandroid-task

How to preserve current back stack (or task) when notification is clicked?


In my application, I create a notification which starts Details Activity. I want to add this activity to top of current task (or back stack). For example I expect application task (back stack) to behave like this:

enter image description here

but I get this:

enter image description here

I have not used FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags. What should I do?

Edit: First picture is just an example. I think the the question's title is completely explicit. I want to add Details Activity on top of current stack, and not to start with a new task.

This is how I create the PendingIntent:

    // Details activity intent
    Intent intent = new Intent(context, DetailsActivity.class);
    intent.putExtra(Com.KEY_ID, event.getId());
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

And this is the manifest:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name_system"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".NoteActivity"
        android:label="@string/app_name_system"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateHidden" />

    <activity
        android:name=".DetailsActivity"
        android:launchMode="singleTop"
        android:label="@string/app_name_system"
        android:theme="@style/AppTheme.NoActionBar" />

Solution

  • I found this link: Preserving Navigation when Starting an Activity. Although it does not provide the exact solution for my question, but it produces the desired result.

    The link describes that we should start DetailsActivity in a new task with a different affinity.

    Manifest:

        <activity
            android:name=".DetailsActivity"
            android:excludeFromRecents="true"
            android:label="@string/app_name_system"
            android:launchMode="singleTop"
            android:taskAffinity=""
            android:theme="@style/AppTheme.NoActionBar" />
    

    PendingIntent creation:

        Intent intent = new Intent(context, DetailsActivity.class);
        intent.putExtra(Com.KEY_ID, event.getId());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);