【问题标题】:Android: resume app from previous positionAndroid:从以前的位置恢复应用程序
【发布时间】:2012-09-06 15:40:59
【问题描述】:

我怎样才能从以前的位置恢复我的应用程序。

请注意,它仍然处于活动状态,只是暂停了。因此,如果我单击 androids current app 按钮或应用程序图标,它会恢复正常。

但我应该从我的小部件中执行此操作..

我有以下几点:

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

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

有人知道怎么做吗?

编辑:

澄清一下,我不想要任何特别的东西。我基本上想模仿按下 android 图标启动器。

【问题讨论】:

  • this SO问题(可能重复)
  • 请不要试图弄乱启动模式来完成这项工作。没有必要,以后会造成更多麻烦。

标签: android android-intent


【解决方案1】:

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

只需模拟启动应用时 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 应该只是将应用程序的现有任务从后台带到前台,而无需实际创建活动的实例。先试试这个。如果它不适合你,做另一个。

【讨论】:

  • 头痛消失了。谢啦! :) 它现在恢复它上次离开的任何活动:)
  • ACTION_MAIN 解决方案并非适用于所有设备。刚测试没有三星设备支持!
【解决方案2】:

使用它就像 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);

【讨论】:

  • 应该是PendingIntente.getActivity
猜你喜欢
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多