Android Galaxy S4 — Activity that is visible over lock screen

I figured it out, and the answer was very different from what I expected.

This piece of code was included in the alarm clock sample from Android 2, in the AlarmAlert.java Activity:

@Override
protected void onStop() {
    super.onStop();
    // Don't hang around.
    finish();
}

For reference, you can see the file from the example code in Git’s past right here, containing the above onStop function. It never caused a problem in Android 2.

But in Android 4, if the phone was off, this onStop would fire right before the phone woke up, effectively “minimizing” the Activity. Once I removed this function, it immediately worked again.

But I wonder, is this the problem that other people like @radley and @Guardanis are getting? It seems unlikely, but please let me know if this fixes your problems too.

If you’re visiting this answer in the future, and you’re getting this problem, what I would try is:

  1. Take out any onStop functions.

  2. Add this code to the Activity:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    
  3. Make sure you’re using a full screen theme, and not a dialog theme.

  4. This didn’t make a difference for me, but you could try setting showOnLockScreen explicitly in the manifest: <activity android:name="com.example.MyActivity" android:showOnLockScreen="true"/>

  5. A second thing that didn’t make a difference for me but you might try is adding the flag WindowManager.LayoutParams.FLAG_FULLSCREEN

I hope this helps other people!

Not sure if this is the problem in all cases, but the documentation on ShowWhenLocked says it applies only to the top-most full-screen window. I had a window themed as a dialog which was not working, but it worked fine once I changed it to a regular full-screen window.

One of the questions you linked to has an answer that appeared to solve this issue for me.

This is the code I am using which appears to be working:

@Override
public void onAttachedToWindow() {
        Window window = getWindow();

        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onAttachedToWindow();
    }

I’m also explicitly declaring this in the activity definition in the manifest:

<activity 
    android:name="com.example.MyActivity"
    android:label="@string/app_name"
    android:showOnLockScreen="true"
    >

Android activity over default lock screen

Leave a Comment