我想要从状态栏通知中恢复我的应用程序,其方式与用户在启动程序中点击其图标时完全相同。
也就是说:我希望堆栈处于与用户离开之前相同的状态。
在通知上设置挂起意图时的问题是,它总是以特定的活动为目标。我不想这样。我需要恢复应用程序,就像启动程序一样。
因此,如果用户在活动A中,我希望恢复活动A。如果用户从活动A启动了活动B,那么当用户点击通知时,我希望显示B,并恢复堆栈,这样当用户点击B中的后退按钮时,A就会恢复。
还有几个类似标题的问题,但都没有解决我的问题。
发布于 2011-04-01 00:14:22
只需使用与Android在启动应用程序时使用的相同意图过滤器:
final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
由于您创建的用于从通知栏打开Activity
的Intent
与用于启动应用程序的安卓相同,因此将显示之前打开的Activity
,而不是创建新的one。
发布于 2015-04-24 20:41:30
我也遇到了同样的问题,并尝试解决这个问题,就像@GrAnd的答案一样:
final Intent notificationIntent = new Intent(context,YourActivity.class);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
这是可行的,毫无疑问,但问题是当您将意图设置为ACTION_MAIN时。然后,您将无法将任何绑定设置为intent。我的意思是,不会从目标活动接收原始数据,因为ACTION_MAIN不能包含任何额外的数据。
相反,您可以只将您的活动设置为singleTask,并正常调用您的意图,而无需设置ACTION_MAIN,并从目标活动的onNewIntent()方法接收intent。
但请注意,如果调用super.onNewIntent(Intent),则将创建该活动的第二个实例。
发布于 2020-08-20 17:53:33
终极解决方案:Notification to restore a task rather than a specific activity?
public class YourRootActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (!isTaskRoot()) // checks if this root activity is at root, if not, we presented it from notification and we are resuming the app from previous open state
{
val extras = intent.extras // do stuffs with extras.
finish();
return;
}
// OtherWise start the app as usual
}
}
https://stackoverflow.com/questions/5502427
复制相似问题