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:
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.
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.
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.
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.
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
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 .... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With