问题
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:
but I get this:
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" />
回答1:
You can use PendingIntent.getActivities
instead of PendingIntent.getActivity
to build you stack.
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);
This should solve your purpose.
Refer this link for more details Back to main activity from notification-created activity
回答2:
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);
回答3:
You need to use getActivities() and specify the entire navigation path in the third argument which accepts an array of intents. You need to construct that array in the order your app navigation should work, i.e., leftmost being the root/main activity and further down the levels from there.
Something like this
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);
Note that : If your application is already active, once you press back android will take you back to your NoteActivity
and then MainActivity
. It will restart the activities if they are already there or create new otherwise.
回答4:
I wanted the exact opposite (which is the default behaviour).
It turns out setting noHistory="true"
or/and calling finish()
on the Activity launched by the notification, keeps the existing backstack.
So basically the solution is either to not keep the Activity into the backstack or create a 'NotificationActivity' which will not be kept and will be in charge of starting the Activity you want (DetailsActivity in your example).
来源:https://stackoverflow.com/questions/42142886/how-to-preserve-current-back-stack-or-task-when-notification-is-clicked