Activity starts but isn't visible when the screen is off or locked

Viewed 176

I'm creating a reminder application, almost everything works, except the activity that is started when the alarm goes off doesn't show up when the screen is off (the phone is in sleep). When the screen is on, seeing the Android's main desktop, and the alarm goes off the activity appears perfectly fine.

I use AlarmManager.setExactAndAllowWhileIdle(RTC_WAKEUP, ...) to set an alarm, which is invoked at a certain time.

This is how I receive the intent from the AlarmManager:

@Override
public void onReceive(Context context, Intent intent) {
    int alarmId = intent.getIntExtra(Constants.REMINDER_ALARM_ID, 0);

    Intent goingOffIntent = new Intent(context, GoingOffActivity.class);
    goingOffIntent.putExtra(Constants.REMINDER_ALARM_ID, alarmId);

    context.startActivity(goingOffIntent);
}

And this is the onCreate of the GoingOffActivity:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_going_off);
context = this;

getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
        WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

loadData(getIntent());

When the screen is off and the alarm goes off these entries created in the log:

18:35:03.593 20113-20113/me.mitulat.myProgram W/MyProgram: onCreate
18:35:03.661 20113-20113/me.mitulat.myProgram W/MyProgram: onCreate finish
18:35:03.674 20113-20113/me.mitulat.myProgram W/MyProgram: onPause
18:35:03.675 20113-20113/me.mitulat.myProgram W/MyProgram: onPause finish
18:35:03.714 20113-20113/me.mitulat.myProgram W/MyProgram: onStop
18:35:03.724 20113-20113/me.mitulat.myProgram W/MyProgram: onStop finish

The GoingOffActivity finishes immediately.

When the screen is on but it's locked with a pattern this happens:

18:30:00.503 20113-20113/me.mitulat.myProgram W/MyProgram: onCreate
18:30:00.585 20113-20113/me.mitulat.myProgram W/MyProgram: onCreate finish
18:30:00.598 20113-20113/me.mitulat.myProgram W/MyProgram: onPause
18:30:00.599 20113-20113/me.mitulat.myProgram W/MyProgram: onPause finish
18:30:00.635 20113-20113/me.mitulat.myProgram W/MyProgram: onStop
18:30:00.655 20113-20113/me.mitulat.myProgram W/MyProgram: onStop finish
18:30:00.764 20113-20113/me.mitulat.myProgram W/MyProgram: onStop
18:30:00.769 20113-20113/me.mitulat.myProgram W/MyProgram: onStop finish

I am testing on real devices, with a minSdkVersion of 23 set in build.gradle.

What do I do wrong?

1 Answers

The double-lifecycle event referenced here is a known limitation of the "window flags" approach to dismissing the keyguard, and wasn't fixed until API 27. What this means is that the Activity can pause & stop before it ever shows up on the screen.

If you don't finish() the Activity, it'll usually onResume() a second time, and everything will be fine. But if you do call finish() in onPause() or onStop(), that's going to cause the Activity to proceed to the destroyed state, and you'll never see it.

The solution is to remove any finish() calls that take place before the Activity becomes visible.

Related