安卓:恢复应用程序至上次位置

10

如何使我的应用从之前的位置继续。

请注意,它仍然处于活动状态,只是暂停了。因此,如果我点击安卓当前应用按钮或应用程序图标,则可以很好地恢复。

但我该如何从我的小部件中实现这一点。

我有以下内容:

// Create an Intent to launch Activity
Intent intent = new Intent(context, LoginForm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

这显然会启动LoginForm而不是仅恢复应用程序。

有人知道如何做到这一点吗?

编辑:

只是为了澄清,我不想要任何特殊的东西。我基本上想模仿按下Android图标启动器的操作。


请查看此 SO 问题(可能是重复的):https://dev59.com/U3A75IYBdhLWcg3wfpGr - Luke
请不要尝试使用启动模式来使其工作。这是不必要的,并且会在以后造成更多麻烦。 - David Wasser
2个回答

19

你已经基本回答了自己的问题 ;-)

只需要模拟 Android 在启动应用时所做的操作:

Intent intent = new Intent(context, LoginForm.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

或者您可以尝试这样做(假设LoginForm是您的应用程序的根活动,并且该活动的一个实例仍然在任务堆栈中处于活动状态):

Intent intent = new Intent(context, LoginForm.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

FLAG_ACTIVITY_NEW_TASK设置为应该只是将应用的一个现有任务从后台带到前台,而不会实际创建活动的实例。首先尝试这个。如果对您无效,请尝试其他方法。


1
头疼消失了。谢谢,伙计! :) 它现在恢复到上次离开的任何活动中 :) - IAmGroot
ACTION_MAIN解决方案并不适用于每一种设备。刚刚测试发现没有三星设备支持它! - DarkLeafyGreen

0
使用这个,它与Android为您的启动器活动所做的相同。
Intent notificationIntent = new Intent(context, SplashActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent clickActionIntent = PendingIntent.getService(context, 0, notificationIntent, 0);

应该是 PendingIntent.getActivity 吗? - pedja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接