简体   繁体   English

从通知恢复android中的活动

[英]Resuming an activity in android from notification

I'm developing an android application, and in one activity, when the user presses the HOME button, a notification appears. 我正在开发一个android应用程序,并且在一个活动中,当用户按下HOME按钮时,会出现一条通知。 Upon pressing the notification the activity would resume from where it left off. 按下通知后,活动将从上次中断的地方恢复。 The problem is that the activity is recreated each time. 问题是每次都会重新创建活动。 Upon pressing the notification onDestroy() and then onCreate() is called. 按下通知后,将调用onDestroy() ,然后调用onCreate() But say if I reopen the activity through the "Recent apps" it resumes successfully. 但是说,如果我通过“最近的应用程序”重新打开活动,它将成功恢复。

This question has been asked before on stackoverflow and I have tried the answers provided and none of them seem to work. 之前在stackoverflow上已经问过这个问题,我已经尝试了提供的答案,但似乎都没有用。 The API that I'm using is the latest 4.4. 我使用的API是最新的4.4。 The options I have tried are: 我尝试过的选项是:

  • Playing around with the Intent flags 玩意向标记

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);

  • Setting the activity to singleInstance and singleTask 将活动设置为singleInstancesingleTask

How can I mimic the intent that is sent when I open my activity through 'Recent apps'? 我如何模仿通过“最近的应用”打开活动时发送的意图?

EDIT: One more caveat that might be important. 编辑:可能需要注意的另一项警告。 There's 3 activities in my program AB C. When clicking on the launcher icon activity A is first started. 我的程序AB C中有3个活动。单击启动器图标时,活动A首先开始。 The notification is created only when going from activity C to Home. 仅当从活动C转到主页时才创建通知。 I'm trying to resume activity C. 我正在尝试恢复活动C。

Problem solved. 问题解决了。

I was debugging the application through LogCat and looking at the intents that were being generated by the ActivityManager. 我正在通过LogCat调试应用程序,并查看ActivityManager生成的意图。 Whenever I was changing the flags of the Intent, I wouldn't see the change in LogCat. 每当我更改Intent的标志时,我都不会在LogCat中看到更改。 So 所以

02-12 11:26:39.586: I/ActivityManager(766): START u0 {flg=0x1000c000 cmp=com.example.app/.controllers.Activity3 bnds=[0,153][1080,345]} from pid -1

Changing the Intent flags didn't change the flg variable in the log. 更改Intent标志不会更改日志中的flg变量。 Apart from this, the notification image was staying in the Status bar, my app wasn't destroying it. 除此之外,通知图像停留在状态栏中,我的应用程序并未销毁它。 What I did was add the PendingIntent.FLAG_CANCEL_CURRENT flag to the PendingIntent I was sending: 我所做的是将PendingIntent.FLAG_CANCEL_CURRENT标志添加到我要发送的PendingIntent中:

    Intent intent = new Intent(this, Activity3.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
    mBuilder.setContentIntent(pendingIntent);

So the problem was an incorrect PendingIntent that I must have created before, that was never being removed. 所以问题是我必须已经创建了一个不正确的PendingIntent,并且从未删除过。 Now I'm able to resume the activity by clicking the notification. 现在,我可以通过单击通知来恢复活动。 Hope this helps someone that might have a similar issue. 希望这对可能遇到类似问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM