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.
One possible solution might be setting a flag while showing the system-dialog and then in the onStop method of the activity life-cycle, check for the flag, if true, finish the activity.
For example, if the system dialog is triggered by some buttonclick, then the onclick listener might be like
and in onstop of activity:
If you use
finish()
just to avoid new app to starts in the stack (task) of you app, you can useIntent.FLAG_ACTIVITY_NEW_TASK
flag, when starting new application and do not callfinish()
at all. According to the documentation, this is the flag to be used to implement a "launcher" style behavior.Here is a solution using
Application
class.You can simply use it like follows,
If you have a reference to the required Activity or using the canonical name of the Activity, you can find out whether it's in the foreground or not. This solution may not be foolproof. Therefore your comments are really welcome.
I don't know why nobody talked about sharedPreferences, for Activity A,setting a SharedPreference like that (for example in onPause() ) :
I think this is the reliable way to track activities visibilty.
Would
Activity.onWindowFocusChanged(boolean hasFocus)
be useful here? That, plus a class-level flag, something likeisFocused
thatonWindowFocusChanged
sets, would be an easy way to tell at any point in your activity if it is focused or not. From reading the docs, it looks like it would properly set "false" in any situation where the activity isn't directly in the physical "foreground", like if a dialog is being displayed or the notification tray is pulled down.Example:
Why not use broadcasts for this? the second activity (the one that needs to be up) can send a local broadcast like this:
then write a simple receiver within the splash activity:
and register your new receiver with the LocalBroadcastManager to listen to the broadcast from your second activity:
NOTE that you could use a constant or a string resource for the "broadcast identifier" string.