Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if activity is in foreground or in visible background?

I have a splash screen on a timer. My problem is that before I finish() my activity I need to check that the next activity has started because a system dialogue box pops-up and I only want to finish(); once the user has selected an option from the dialogue box?

I know that there are many questions on how to see if your activity is in the foreground but I do not know if this allows for dialogue boxes on top of the activity too.

Here is the problem, the red is my activity which is in the background while the dialogue is in the foreground:

the red is my activity which is in the background while the dialogue is in the foreground

EDIT: I have tried just not using finish() but then my activity can be gone back to in the stack of applications which I am trying to avoid.

like image 457
Nick Avatar asked Aug 03 '13 23:08

Nick


People also ask

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

It's very easy to detect when an Activity goes background/foreground just by listening to the lifecycle events, onStop() and onStart() of that Activity.

How do you know if activity is visible or not?

In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.

What is foreground activity and background activity?

Foreground refers to the active apps which consume data and are currently running on the mobile. Background refers to the data used when the app is doing some activity in the background, which is not active right now.


2 Answers

This is what is recommended as the right solution:

The right solution (credits go to Dan, CommonsWare and NeTeInStEiN) Track visibility of your application by yourself using Activity.onPause, Activity.onResume methods. Store "visibility" status in some other class. Good choices are your own implementation of the Application or a Service (there are also a few variations of this solution if you'd like to check activity visibility from the service).

Example Implement custom Application class (note the isActivityVisible() static method):

public class MyApplication extends Application {    public static boolean isActivityVisible() {     return activityVisible;   }      public static void activityResumed() {     activityVisible = true;   }    public static void activityPaused() {     activityVisible = false;   }    private static boolean activityVisible; } 

Register your application class in AndroidManifest.xml:

<application     android:name="your.app.package.MyApplication"     android:icon="@drawable/icon"     android:label="@string/app_name" > 

Add onPause and onResume to every Activity in the project (you may create a common ancestor for your Activities if you'd like to, but if your activity is already extended from MapActivity/ListActivity etc. you still need to write the following by hand):

@Override protected void onResume() {   super.onResume();   MyApplication.activityResumed(); }  @Override protected void onPause() {   super.onPause();   MyApplication.activityPaused(); } 

In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.

The source also mentions two wrong solutions...so avoid doing that.

Source: stackoverflow

like image 187
But I'm Not A Wrapper Class Avatar answered Sep 20 '22 13:09

But I'm Not A Wrapper Class


If targeting API level 14 or above, one can use android.app.Application.ActivityLifecycleCallbacks

public class MyApplication extends Application implements ActivityLifecycleCallbacks {     private static boolean isInterestingActivityVisible;      @Override     public void onCreate() {         super.onCreate();          // Register to be notified of activity state changes         registerActivityLifecycleCallbacks(this);         ....     }      public boolean isInterestingActivityVisible() {         return isInterestingActivityVisible;     }      @Override     public void onActivityResumed(Activity activity) {         if (activity instanceof MyInterestingActivity) {              isInterestingActivityVisible = true;         }     }      @Override     public void onActivityStopped(Activity activity) {         if (activity instanceof MyInterestingActivity) {              isInterestingActivityVisible = false;         }     }      // Other state change callback stubs     .... } 
like image 28
silvermouse Avatar answered Sep 20 '22 13:09

silvermouse