I want to lock my application when it goes in background and when it resumes I want to display my own lock screen. The lock screen is an Activity of my application.
Aft
The principal problem is that you have to get a specific behavior when you start an Activity
from the background. onPause()
and onResume()
are called every time you switch between Activities
and not just when you minimize your application. To get around that you can get the currently running tasks from the ActivityManager
and compare their package name to the one of your application:
private boolean isApplicationInBackground() {
final ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> tasks = manager.getRunningTasks(1);
if (!tasks.isEmpty()) {
final ComponentName topActivity = tasks.get(0).topActivity;
return !topActivity.getPackageName().equals(getPackageName());
}
return false;
}
Now you can do this:
public boolean wasPaused = false;
@Override
public void onPause() {
super.onPause();
if(isApplicationInBackground()) {
wasPaused = true;
}
}
@Override
public void onResume() {
super.onResume();
if(wasPaused) {
lockScreen();
wasPaused = false;
}
}
And that is it! I recommend you implement this in a base Activity
which all your other Activities
inherent from.
Your activity has an onResume()
callback in its life cycle. Simple override this and do what you need to do.
You can achieve that if you have a global Activity "MyActivity" and all the activities extend from it.
Then you override onPause and onStop methods on "MyActivity"
@Override
public void onPause()
{
super.onPause();
setLockStatus(false);
}
@Override
public void onStop()
{
super.onStop();
setLockStatus(true);
}
and:
@Override
public void onResume()
{
super.onResume();
checkLockScreen();
}
EDIT: Obviously you need to create the methods setLockStatus and checkLockScreen and do whatever you want (like save the status on sharedPreferences).
Well you should override methods onResume and onPause
see this and you will get it :)
in your activity you can have a flag like boolean locked;
and in onPause you can set this to true. And every time in critical places you can check the state of this flag and if it is true then you show you unlock activity, after a success in unlocking then set the flag to false.
For anyone that is interested, if I understand your question correctly, I was looking into similar functionality and wasn't happy with what I found. I wanted to determine when the activity was being resumed from the background vs. when it was being resumed from a called activity.
I ended up utilizing a boolean flag that get's set from startActivity that will allow the calling Activity to determine whether or not it is resuming from the called Activity or from the background. Something like this
private static boolean RESUME_FROM_ACTIVITY;
@Override
public void onResume(){
super.onResume();
if(!RESUME_FROM_ACTIVITY){
// do what you want like lock the screen
}
RESUME_FROM_ACTIVITY = false;
}
@Override
public void startActivity(Intent intent){
RESUME_FROM_ACTIVITY = true;
super.startActivity(intent);
}
Because of the way that Android handles statics and the activity stack (read more in this blog post), this should work conceptually. A bit more of an explanation, the static should be one of the last things that gets cleared from memory when your app is running if the heap space gets too large. The activity will not be destroyed without the stack itself being destroyed, so onResume will be called and it will check the static flag to see if it is coming from another activity or from the background.
The static is likely even overkill, as again the activity itself won't be destroyed and it's globals will be held as long as possible. This is only conceptual and we are still in the middle of stress testing it, but it is an option. If we find any issues I will update this post.