William Melani
William Melani

Reputation: 4268

Android: Tell When App Returns to Foreground

I have an activity called EntranceActivity which routes to other activities based on a few different parameters.

This Activity is ClearTop-ed, such that I can startActivity(...EntranceActivity...) and re-route through the EntranceActivity logic without having to worry about little nuances. It is also the only entrance point to the application when returning to foreground.

I need to be able to determine when this Activity is being pushed to the foreground from the beginning (from first launch / from being in background) vs when I am routing to it during the app usage. (i.e. from Activity2 back to EntranceActivity)

The following question shows how to determine if the app is in the foreground... but the EntranceActivity will only execute when the app is in the Foreground, so that does not seem to be what I'm looking for.

How can I tell if android app is running in the foreground?

Upvotes: 0

Views: 836

Answers (2)

William Melani
William Melani

Reputation: 4268

I ended up getting this to work by adding another EntranceActivity which is the only entry point to the app, and redirects to the ViewDispatcherActivity (the old EntranceActivity).

Now I can continue to clear-top to ViewDispatcherActivity as needed during the app, while having some Return-To-Foreground logic (because the user never returns to EntranceActivity during normal app operation)

EntranceActivity (the only app entry point)

onCreate:

if (ShouldShowAccessActivity()) { startActivityForResult( new Intent(EntranceActivity.this,AccessActivity.class)); return; }

//go to view dispatcher to further branch off.
startActivityForResult(new Intent(EntranceActivity.this,ViewDispatcherActivity.class));

Upvotes: 0

Joel Martinez
Joel Martinez

Reputation: 47749

You can easily do this with refcounting ... in your base activity (you have one right? ;-) ), increment the value in the onResume method, and decrement it in the onPause method. If the value is non-zero, then your app is visible :-)

Upvotes: 1

Related Questions