Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if my Application is resumed from background?

Tags:

android

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.

After successfully entring password the user can see the resumed Activity else he can't.

How can I do this?

like image 320
Dharmendra Avatar asked Oct 31 '11 10:10

Dharmendra


People also ask

How do I know if an app is running in the background?

To see what apps are running in the background, go to Settings > Developer Options > Running Services.

How do I know if an app is running in the background or foreground?

Whenever a new activity is started and visible to the user its instance is updated in the application class: activeActivity variable, so we can use it to determine which activity is in the foreground from the notifications layer.

What happens when app goes into background?

An app is running in the background when both the following conditions are satisfied: None of the app's activities are currently visible to the user. The app isn't running any foreground services that started while an activity from the app was visible to the user.

Which activity life cycle event is triggered when an application is opened from background?

onStart() It is invoked when the activity is visible to the user. It is followed by onResume() if the activity is invoked from the background.


1 Answers

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.

like image 95
zgc7009 Avatar answered Oct 13 '22 00:10

zgc7009