Why does a PendingIntent trigger onCreate even though FLAG_NO_CREATE is set

时间秒杀一切 提交于 2019-12-13 02:35:44

问题


One of my Activities starts an AsyncTask and adds an ongoing notification to the notification area. This notification receives updates from within the AsyncTask.

I did this so the users are able to leave the Activity with the Home button and still receive progress updates during a long running operation.

Clicking the notification should return the user to this activity. The problem is that no matter what, it seems that the old activity is never used again, but instanciated new (while the AsyncTask still runs). The onCreate method of this activity gets called every time the notification gets clicked.

I tried using the FLAG_NO_CREATE and FLAG_UPDATE_CURRENT options, but it doesn't make any difference.

What is the correct way to handle this situation? I'm developing for API level 4.


回答1:


I did this so the users are able to leave the Activity with the Home button and still receive progress updates during a long running operation.

Sounds like you should use a service.

The problem is that no matter what, it seems that the old activity is never used again, but instanciated new

Yes. Android probably killed your activty. You have ABSOLUTELY NO guarantee what happens to your activity after its onStop is called.

I'm developing for API level 4

Any particular reason you're writing for 4? That's pretty old and most phones are on 2.0 now.



来源:https://stackoverflow.com/questions/4683619/why-does-a-pendingintent-trigger-oncreate-even-though-flag-no-create-is-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!