开发者

How to handle Android activity lifecycle on wake up (alarm app)

开发者 https://www.devze.com 2023-01-18 11:13 出处:网络
I\'m 开发者_开发技巧having a couple of problems with an alarm app I am developing. The first thing that I think is a bit weird is that when an alarm goes of and wakes the phone up. These things happe

I'm 开发者_开发技巧having a couple of problems with an alarm app I am developing.

The first thing that I think is a bit weird is that when an alarm goes of and wakes the phone up. These things happend.

oncreate

onresume

onpause

onresume

Why are they run in that order? The last two should not be called? And this is what's causes me big trouble, because when i press home or back on the phone, onPause is run, which I want to call finish() from. And that part works as it should, but that does not work when the phone wakes upp from sleep bacause of the onPause call...

Any ideas?


I also have a similar problem: i create an Activity and a Service in the background. When the service receive some data from the net it will bring-up the activity. Everything works fine until the activity in in the onStop phase and the screen goes black. When I have this situation and I request the activity to restart, I have:

onRestart onStart onResume onPause onNewIntent onResume

As you can see I have an onResume, onPause and another onResume and the activity came to the user.

Seems that is because we use a singleTop or singleInstance Activity (maybe also appear on singleTask). The problem is that when you send an Intent to an already created singleTop Activity, your new intent active the onNewIntent() method. from the API you can see that:

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

so maybe this is why you receive onResume-onPause-onResume calls.

However this is a strange behavior!


I would suggest you look at how the official alarm application handles the events: https://android.googlesource.com/platform/packages/apps/DeskClock


Do you really want to call finish() in onPause()? onPause() is called when the current activity is not in the foreground any more. Maybe you want to call finish() in your onStop() method instead. See the Activity lifecycle for details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号