Lisa Anne
Lisa Anne

Reputation: 4595

How to restart same Activity immediately after HOME button is pressed?

I am trying to develop a custom "lock screen".

The main issue I am facing is that the user can actually always exit my "lock screen" Activity simply by pressing the HOME button.

I know it is not possible to block or override HOME. I was wondering if there is a way to restart the same Activity when the user presses HOME.

I have tried to restart the activity from the onPause(), indeed the Activity restarts, but it takes some seconds to restart, so the "lock screen" is pointless.

Is there some way to restart the Activity immediately after Home is pressed?

Upvotes: 1

Views: 1726

Answers (1)

fweigl
fweigl

Reputation: 22018

I had the same problem once and I created a Service that was running in the background. It had a TimerTask loop that ran every second and checked wether my LockScreenActivity was in the foreground. You can do that like this:

List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(4096); 
    return runningTasks.get(0).topActivity.getClassName();

If my lockscreen wasn't in the foreground, I started it up/ brought it to the foreground again (make it singleTop in the Manifest).

TronicZomB's answer looks more sophisticated though.

Upvotes: 3

Related Questions