0

Today I noticed something rather interesting. The activity lifecycle seems to have a slight discrepancy when the phone display times out. Let me explain with an example.

I have an activity that uses the accelerometer to vibrate the phone. In the onPause event I unregister the SensorManager listener so that I don't vibrate the phone when my activity is no longer the main focus.

However, I've noticed if the display shuts off and then comes back on my activity has the SensorManager listener registered even before I unlock the screen, enter my password, and my activity is shown.

Now I realize this is my own interpretation of how I would expect it to work, but to me this seems rather strange, since my activity isn't yet the main focus. I expected the SensorManager listener wasn't registered because onResume hasn't yet been called. This clearly isn't the case when I can make my phone vibrate from both the lock screen and the password screen.

So, can anyone explain why this behavior? Secondly what can I do to get around it?

Thank you.

EDIT for clarity

I use the accelerometer to trigger a vibrate by moving the phone. This is accomplished through the SensorManager listener.

Scenario:

My activity is in the foreground (main focus). I trigger the vibrate by moving the phone. The display times out. At this point I can not trigger the vibrate. I press home/power to show the screen. I can now vibrate my phone even though either the lock screen or password screen is shown and my activity is not in the foreground.

I can not verify if the reason I can not vibrate the phone when the display turns off is because onPause was called or there is something inherent to the OS that prevents it. Or to put it another way, I also can't verify if onResume was called when the display turned on.

The key to all this is understanding the activity life cycle when the phones display is shut off. Unfortunately, my expectation was that it would follow the same life cycle diagram that we have all learned. My opinion was that it is different.

user432209
  • 20,007
  • 10
  • 56
  • 75
  • Sounds like the lock screen doesn't actually push your code to the background. It's hard to understand your question. Are you saying that the screen locks with your activity in focus, and you EXPECT onPause to get called but it's not? – Falmarri Jan 30 '11 at 02:32
  • @Falmarri - updated for clarity. I hope this answers your question. – user432209 Jan 30 '11 at 02:43
  • I'm having the same issue. https://stackoverflow.com/questions/54652630/lock-screen-triggers-application-lifecycle-events-of-different-activity – Jim Clermonts Feb 13 '19 at 08:57

3 Answers3

2

You said it.

"expected the SensorManager listener wasn't registered because onResume hasn't yet been called. This clearly isn't the case when I can make my phone vibrate from both the lock screen and the password screen."

onResume is called before you unlock the phone

mattias
  • 21
  • 2
2

The following code will handle Screen Off and Screen On intents. Perhaps you can call onPause() in the onResume() called only after the screen turns on, and call and onResume() when your activity has focus again.

public class ExampleActivity extends Activity {

   @Override
    protected void onCreate() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause() {
        if (ScreenReceiver.wasScreenOn) {
            System.out.println("onPause() called because screen turned off.");
        } else {
            System.out.println("normal onPause() call");
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        if (!ScreenReceiver.wasScreenOn) {
            System.out.println("onResume() called when screen turns on");
        } else {
            System.out.println("normal onResume() call");
        }
        super.onResume();
    }

}
0

Ah. Your edit tells us why this is happening.

The problem is that when your "screen times out" and the phone goes black, what is happening is the phone is actually going to sleep. The OS is probably going into a low power state and turning of listeners for various things and possibly turning off the accelerometer and/or vibrator.

If you really want the activity to be able to vibrate, try holding a wake lock.

http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • Actually it is the opposite. I don't want the activity to be able to vibrate, I want my listener to get unregistered. Unfortunately, it looks like when the phone goes to sleep, onPause isn't called. Is there a sleep event I can use to unregister my listener? – user432209 Jan 30 '11 at 03:01
  • Forgot to add.... I then want to re-register my listener once my activity is the main focus...NOT when the power button is pressed and the screen lock is shown. Basically, if my activity isn't shown, I don't want to my listener to be registered...this is why this whole debacle is throwing me for a loop. – user432209 Jan 30 '11 at 03:08
  • Hmm. I'm not sure of the best way to do what you want. Your best bet is probably to register for the screen lock/screen unlock broadcasts and unregister/re-register your listener there. – Falmarri Jan 30 '11 at 03:30