Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if any of my activity is front-most and visible to user?

I would like to launch an intent when any of my activity is visible, otherwise I will put it up as a notification, and will be fired by the user.

To decide this, I need to know if any of my activity is front-most, how do I that?

like image 681
Pentium10 Avatar asked Jun 28 '10 21:06

Pentium10


People also ask

How do you know if activity is visible or not?

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.

When an activity is visible to the users which method is called?

2. onStart() The Android onstart() method is invoked as soon as the activity becomes visible to the users. This method is to start an activity.

How do I find my main activity?

This can be found in the application's manifest. The main activity is the activity with the intent-filter whose name is android. intent. action.


4 Answers

I don't know that there's a method to get the currently displayed activity, but you could do something combining the Activity Lifecycle and a flag.

For the flag, if you've extended the Application class, that's probably a decent place to store it. For extending the application class, the top answer to this question has info. (d).

So probably keep track of the current active activity (or a flag that the activity is visible) in onResume/onPause or onStart/onStop depending on exactly what behavior you want.

Since you have multiple activities, you'll need a centroid place for storing the flag, which is why the Application makes sense. You can get the custom Application object by casting the application context (e.g. ((MyApplication)getApplicationContext()).isMyActivityActive).

You could extend Activity as well to help keep this code clean and contained.


If you're using a service you could bind to the service in every activity in the onStart/onStop (or onResume/onPause). If bound, you're visible.

like image 199
mbafford Avatar answered Oct 04 '22 09:10

mbafford


You can ask for the running tasks from ActivityManager:

ActivityManager activityManager = (ActivityManager)getContext()
                .getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE));

From API docs:

Return a list of the tasks that are currently running, with the most recent being first and older ones after in order.

So the first item on the list is the frontmost activity.

like image 36
Zds Avatar answered Oct 04 '22 08:10

Zds


onResume() called && onPause() not called = visible.
Have a public static Activity currentlyVisible; in your Application subclass that will be updated by your activities (set to the instance in onResume() and nulled in onPause()). Or invent a less ugly variant of a registry.

like image 34
yanchenko Avatar answered Oct 04 '22 09:10

yanchenko


You could use onWindowFocusChanged(boolean hasFocus) place this in a superclass of your activities to launch the intent if it has focus.

like image 41
Grant Avatar answered Oct 04 '22 09:10

Grant