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.
Save a flag if you are paused or resumed. If you are resumed it means you are in the foreground
I used to do like,
if the activity is not in the foreground
will return null. :=P
I think I have better solution. Because you can build in simply MyApplication.activityResumed(); to every Activity by one extend.
Firstly you have to create (like CyberneticTwerkGuruOrc)
Next, you have to add Application class to AndroidManifest.xml
Then, create class ActivityBase
Finally, when you crate new Activity, you can simply extends it by ActivityBase instead of Activity.
For me It's better method cause you have to just remember about extend by ActivityBase. In addition you can expand your base function in future. In my case I added receivers for my service and alerts about network in one class.
If you wanna check visibility of your App, you can simply call
UPD: updated to state
Lifecycle.State.RESUMED
. Thanks to @htafoya for that.In 2019 with help of new support library
28+
or AndroidX you can simply use:You can read more in the documenation to understand what happened under the hood.
Activity::hasWindowFocus() returns you the boolean you need.
Here is an example class to check your activites' visibility from wherever you are.
Remember that if you show a dialog, the result will be false since the dialog will have the main focus. Other than that it's really handy and more reliable than suggested solutions.
That's exactly the difference between
onPause
andonStop
events of the activity as described in the Activity class documentation.If I understand you correctly, what you want to do is call
finish()
from your activityonStop
to terminate it. See the attached image of the Activity Lifecycle Demo App. This is how it looks like when Activity B is launched from Activity A. The order of events is from bottom to top so you can see that Activity AonStop
is called after Activity BonResume
was already called.In case a dialog is shown your activity is dimmed in the background and only
onPause
is called.