How to save the current back stack (or task) when clicking on the notification?

In my application, I create a notification that triggers an Activity Activity . I want to add this activity to the top of the current task (or back to the stack). For example, I expect the application task (back stack) to behave as follows:

enter image description here

but I understand:

enter image description here

I have not used FLAG_ACTIVITY_CLEAR_TASKand FLAG_ACTIVITY_NEW_TASK. What should I do?

Change: The first image is just an example. I think the title of the question is completely clear. I want to add Detailed activity on top of the current stack, rather than starting with a new task.

This is how I create 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 manifested:

    <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" />
+8
4

: . , .

, DetailsActivity .

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:

    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);
+2

PendingIntent.getActivities PendingIntent.getActivity .

Intent mainActivityIntent = new Intent(context,MainActivity.class);
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Intent noteActivityIntent= new Intent(context,NoteActivity.class);
noteActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Intent detailActivityIntent= new Intent(context,DetailActivity.class);

final PendingIntent pendingIntent = PendingIntent.getActivities(ctx, UNIQUE_REQUEST_CODE++,
            new Intent[] {mainActivityIntent,noteActivityIntent,detailActivityIntent},PendingIntent.FLAG_UPDATE_CURRENT);

.

.

+2

getActivities() , . , , .. / .

-

Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // only specify new task flag for MainActivity

Intent noteActivityIntent= new Intent(context,NoteActivity.class);

Intent detailsActivityIntent = new Intent(context, DetailsActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivities(context, 0,
            new Intent[] {mainActivityIntent,noteActivityIntent,detailsActivityIntent},PendingIntent.FLAG_UPDATE_CURRENT);

, : , , NoteActivity, MainActivity. , .

+1

( ).

It turns out that setting up noHistory="true"and / or calling finish()the Activity triggered by the notification saves the existing backstack.

Thus, the main solution is to not keep the activity in the backstack or create a "NotificationActivity" that will not be saved and will be responsible for triggering the required activity (DetailsActivity in your example).

0
source

Source: https://habr.com/ru/post/1669292/


All Articles