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.
If targeting API level 14 or above, one can use android.app.Application.ActivityLifecycleCallbacks
Use the time gap between pause and resume from background to determine whether it is awake from background
In Custom Application
In BaseActivity Class
Two possible solutions :
1) Activity LifeCycle Callbacks
Use an Application that implements ActivityLifecycleCallbacks and use it to track Activities lifecycle events in you application. Note that ActivityLifecycleCallbacks are for Android api >= 14. For previous Android api, you'll need to implement it by yourself inside all of your activities ;-)
Use Application when you need to share / store states accross activities.
2) Check for running process informations
You can check the status of a running process with this class RunningAppProcessInfo
Fetch the running process list with ActivityManager.getRunningAppProcesses() and filter the result list to check for the desired RunningAppProcessInfo and check its "importance"
This can achieve this by a efficient way by using Application.ActivityLifecycleCallbacks
For example lets take Activity class name as ProfileActivity lets find whether its is in foreground or background
first we need to create our application class by extending Application Class
which implements
Lets be my Application class as follows
Application class
in the above class there is a override methord onActivityResumed of ActivityLifecycleCallbacks
where all activity instance which is currently displayed on screen can be found, just check whether Your Activity is on Screen or not by the above method.
Register your Application class in manifest.xml
To check weather Activity is Foreground or background as per the above solution call the following method on places you need to check
If you want to know if any activity of your app is visible on the screen, you can do something like this:
Just create a singleton of this class and set it in your Application instance like below:
Then you can use isAnyActivityVisible() method of your MyAppActivityCallbacks instance everywhere!
This is what is recommended as the right solution:
In your
finish()
method, you want to useisActivityVisible()
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