0

I figured out that executing phone lock on app startup gets my initial AsyncTask out of step because it doesn't call onStart (where I attach and detach the Activity to the task) after unlock. Obviously phone lock mode means something different to any app than using Home or Back button, because in the latter ones, my app can deal with them. It seems that the locking/unlocking mode doesn't close the app, but it doesn't keep it running too, so in my opinion this is some kind of unexpected behavior which is very stupid because I "lose" my AsyncTask and my app is not getting initialized properly.

How can I execute some logic on phone unlocking, because the unlock/lock doesn't seem to call any lifecycle methods? And do I have to distinguish between lock on/off and screen on/off?

3
  • 1
    "because the unlock/lock doesn't seem to call any lifecycle methods" - Not even onPause() and onResume()?
    – Squonk
    Dec 26, 2012 at 17:18
  • I'm pretty sure onResume() will always get called in this case. But i don't think this is of much help in this case because you can't tell what action triggered it and by the looks of it OP needs to know when the phone has been unlocked.
    – Paul
    Dec 26, 2012 at 17:22
  • Ok, I didn't tested if onResume works, but as Paul already mentioned, you can't tell what action triggered. Moreover I can't put the AsyncTask handling into onResume due to my app's usage.
    – Bevor
    Dec 26, 2012 at 17:32

3 Answers 3

0

I'm pretty sure THIS question will tell you what you want to know.

Basically what you want to do is create a Broadcast Receiver which listens for the following intent:

Intent.ACTION_USER_PRESENT

The intent is sent by the system when the user unlocks the screen. You can check it's documentation HERE.

1
  • I already saw this posting, but I wondered if this is a solution for my needs. But I will think about it, maybe I have to apply some of this techniques.
    – Bevor
    Dec 26, 2012 at 17:33
0

See Activity Lifecycle documentation for a good description of the lifecycle, with diagrams.

Most likely your activity is killed with the screen goes off to save resources (battery power). As the documentation states, you can basically be killed anytime that Android wants to free resources. So, you should always design your activities to be able to be stopped and restarted at any time.
I had a similar issue once. My app was in landscape only, and when turning off the screen, the android screensaver took control (it was in portrait mode), thus sending an orientationChange that destroys and recreates the activity.

A simple solution is to declare that you will manage yourself screen orientation changes:

<activity ... android:configChanges="orientation" ... >

This is quite easy if your activity is declared to be landscape only (you have to do nothing), but can get harder if your activity can rotate...

1
  • My screen orientation is android:screenOrientation="nosensor". This is very helpful since it sets the orientation to the default value of the device and it is not changable. That means is landscape on tablets and portrait on phones. I don't want to change that.
    – Bevor
    Dec 26, 2012 at 17:29
0

I found a solution which works for me. Due to Squonks comment I saw that onResume will be executed. I was able to adapt the logic of my app so my app can handle the AsyncTask (attaching/detaching + showing progress dialog + initialization) in onResume now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.