单击通知后清除任务并导航到主 activity
Clearing task and navigate to main activity upon clicking notification
我正在尝试解决以下情况:
- 应用程序包含两个活动,Activity_A 和 Activity_B
- 目前用户位于Activity_B
- 收到导致 Activity_A
的通知
然而
当按下后退键时,从应用程序中导航出来如下:
Activity_A(来自通知)-> Acivity_B-> Activity_A-> 应用程序关闭。
我想要实现的是,单击通知后,Activity_B 将完成,并且同一任务中的 Activity_A 将呈现给用户。
我经历了无数几乎描述相同场景的答案和问题,但所有答案实施试验都没有解决我的问题。
如果我在我的 main activity 中设置了我想要的东西
android:launchMode="singleInstance"
但随后每个 activity 调用似乎都启动了一个新应用程序。
您可以在下面找到一些代码:
final Intent notificationIntent = new Intent(getApplicationContext(), MyDevices.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,0);
final Notification notification = new Notification.Builder(
getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(mNotificationTitle)
.setOngoing(false)
.setContentText(mNotificationMessage)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL).build();
notificationManager.notify(1,notification);
您是否尝试设置 singleTask 而不是 singleInstance?
您也可以尝试实现 onActivityResult 以在从 activityA.
返回时完成 activityB
如果没有,我建议您查看我的库以处理 activity 导航:https://github.com/yayaa/TheActivityManager
编辑:
为了更好地理解 singleTask 和 singleInstance 之间的区别,您可以从 launchMode 源文档中查看以下描述。
The "singleTask" and "singleInstance" modes also differ from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent
我能看到的唯一警告是以下警告,如果您知道自己在做什么,并且确定自己想要什么,在我看来这并不那么重要。
singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.
已针对多个用例测试您的应用程序,应该没问题。由于您的应用程序收到推送通知,我认为这是您应该采用的方式。
没什么特别的,只需在 B_Activity 中添加此功能即可。只要你离开就完成 activity。这是很好的做法。
protected void onUserLeaveHint() {
try{
super.onUserLeaveHint();
finish();
}catch(Exception ex){
//Something went wrong.
}
}
接受的答案不好。为这种行为使用特殊的启动模式是不正确的,即使它可能适用于这种特定情况。通常使用 singleTask
或 singleInstance
启动模式不是正确的方法,通常会导致比解决的问题更多的问题。
您不需要任何特殊的启动模式来处理这个问题。您只是缺少 Intent.FLAG_ACTIVITY_NEW_TASK
。这应该可以在不使用任何特殊启动模式的情况下正常工作,只需像这样创建通知 Intent:
final Intent notificationIntent = new Intent(getApplicationContext(),
MyDevices.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
对于Intent.FLAG_ACTIVITY_CLEAR_TASK
的documentation也很清楚这一点。它说:
If set in an Intent passed to Context.startActivity(), this flag will
cause any existing task that would be associated with the activity to
be cleared before the activity is started. That is, the activity
becomes the new root of an otherwise empty task, and any old
activities are finished. This can only be used in conjunction with
FLAG_ACTIVITY_NEW_TASK.
我正在尝试解决以下情况:
- 应用程序包含两个活动,Activity_A 和 Activity_B
- 目前用户位于Activity_B
- 收到导致 Activity_A 的通知
然而 当按下后退键时,从应用程序中导航出来如下: Activity_A(来自通知)-> Acivity_B-> Activity_A-> 应用程序关闭。
我想要实现的是,单击通知后,Activity_B 将完成,并且同一任务中的 Activity_A 将呈现给用户。
我经历了无数几乎描述相同场景的答案和问题,但所有答案实施试验都没有解决我的问题。
如果我在我的 main activity 中设置了我想要的东西
android:launchMode="singleInstance"
但随后每个 activity 调用似乎都启动了一个新应用程序。
您可以在下面找到一些代码:
final Intent notificationIntent = new Intent(getApplicationContext(), MyDevices.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,0);
final Notification notification = new Notification.Builder(
getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(mNotificationTitle)
.setOngoing(false)
.setContentText(mNotificationMessage)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL).build();
notificationManager.notify(1,notification);
您是否尝试设置 singleTask 而不是 singleInstance? 您也可以尝试实现 onActivityResult 以在从 activityA.
返回时完成 activityB如果没有,我建议您查看我的库以处理 activity 导航:https://github.com/yayaa/TheActivityManager
编辑:
为了更好地理解 singleTask 和 singleInstance 之间的区别,您可以从 launchMode 源文档中查看以下描述。
The "singleTask" and "singleInstance" modes also differ from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent
我能看到的唯一警告是以下警告,如果您知道自己在做什么,并且确定自己想要什么,在我看来这并不那么重要。
singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.
已针对多个用例测试您的应用程序,应该没问题。由于您的应用程序收到推送通知,我认为这是您应该采用的方式。
没什么特别的,只需在 B_Activity 中添加此功能即可。只要你离开就完成 activity。这是很好的做法。
protected void onUserLeaveHint() {
try{
super.onUserLeaveHint();
finish();
}catch(Exception ex){
//Something went wrong.
}
}
接受的答案不好。为这种行为使用特殊的启动模式是不正确的,即使它可能适用于这种特定情况。通常使用 singleTask
或 singleInstance
启动模式不是正确的方法,通常会导致比解决的问题更多的问题。
您不需要任何特殊的启动模式来处理这个问题。您只是缺少 Intent.FLAG_ACTIVITY_NEW_TASK
。这应该可以在不使用任何特殊启动模式的情况下正常工作,只需像这样创建通知 Intent:
final Intent notificationIntent = new Intent(getApplicationContext(),
MyDevices.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
对于Intent.FLAG_ACTIVITY_CLEAR_TASK
的documentation也很清楚这一点。它说:
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.