Android: how do I check if activity is running?

android check if activity is running from service
android check if activity is running in foreground
android check if activity is destroyed
how to check if app is running in background android
android get current activity
android get current activity name
android activity lifecycle
get all running activities android

Is there any simple way of determining whether or not a certain activity is active? I want to do certain things depending on which activity is active. eg:

if(activityrunning == activity1)
//do this
else if (activityrunning == activity2)
//do something else

You can use a static variable within the activity.

class MyActivity extends Activity {
     static boolean active = false;

      @Override
      public void onStart() {
         super.onStart();
         active = true;
      } 

      @Override
      public void onStop() {
         super.onStop();
         active = false;
      }
}

The only gotcha is that if you use it in two activities that link to each other then onStop on the first is sometimes called after onStart in second. So both might be true briefly.

Depending on what you are trying to do (update the current activity from a service?). You could just register a static listener in the service in your activity onStart method then the correct listener will be available when your service wants to update the UI.

How to check if activity is in foreground or in visible background , How do you check if an activity is already running in Android? In your activity, register a receiver in onStart, unregister in onStop. Now when for example a service needs to handle something that the activity might be able to do better, send an ordered broadcast from the service (with a default handler in the service itself). You can now respond in the activity when it is running.


I think more clear like that:

  public boolean isRunning(Context ctx) {
        ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

        for (RunningTaskInfo task : tasks) {
            if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName())) 
                return true;                                  
        }

        return false;
    }

Android: How to detect when App goes background/foreground, How do you know if activity is visible or not? Before getting into example, we should know what service is in android. Service is going to do back ground operation without interact with UI and it works even after activity destroy. This example demonstrate about How to check if a service is running on Android.


An option without using any auxiliar variable is:

activity.getWindow().getDecorView().getRootView().isShown()

where activity is f.e.: this or getActivity().

The value returned by this expression changes in onStart() / onStop(), which are the events that start / stop showing the layout of the activity on the phone.

2 Ways to find appPackage and appActivity name of your App , . onResume methods. Store "visibility" status in some other class. On your Android phone or tablet, open your device's Settings app Google Manage your Google Account. At the top, tap Data & personalization. Under "Activity and timeline," tap My Activity. At the top right, tap More Other Google activity. Find the activity you want to see.


I used MyActivity.class and getCanonicalName method and I got answer.

protected Boolean isActivityRunning(Class activityClass)
{
        ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

        for (ActivityManager.RunningTaskInfo task : tasks) {
            if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
                return true;
        }

        return false;
}

If you need to check any of the app's activity is running, you can create a base class for your activities and override onPause and onResume. In onPause, call a service method to let it know it is on the background. In onResume, call a service method to let it know it is on the foreground. The code is logical and uses open android APIs in the way they intended to be used (e.g. getRunningTasks should be used to get running tasks and one might want to call it only with the purpose to analyze these tasks).


I realize this issue is quite old, but I think it's still worth sharing my solution as it might be useful to others.

This solution wasn't available before Android Architecture Components were released.

Activity is at least partially visible

getLifecycle().getCurrentState().isAtLeast(STARTED)

Activity is in the foreground

getLifecycle().getCurrentState().isAtLeast(RESUMED)

You can use a static variable within the activity. class MyActivity extends Activity { static boolean active = false; @Override public void onStart()  Yes you can check if activity is active: Refreshing an Activity from service when Active Also, if your are not doing anything when your Activity in not active, then you should probably unregister the listener when your Activity deactivates.


In this way we can find out current running application and task. Use : 1. Suppose you have a task like , when user opens message/phone call/any application  If you have a device running Android 6.0 or above and you go to Settings > Developer options > Running services, you can tap on active apps and choose to Stop. You’ll see a warning if an app cannot


Android. Is there a way to determine if a certain activity is running? ActivityManager is useful for Checking whether a activity is running or not. You can check this out in a few different ways, but here’s the sure-fire winner that works in Android Pie, Oreo, and below: Go to Settings > System > About phone . Scroll down and find “Build


Other cases that can cause configuration changes include changes to language or input device. When a configuration change occurs, the activity is destroyed and  Android :: How To Check If Activity Is Current Activity Running In Screen Jul 16, 2010 I used Toast to make notification, but it seems it will appear even its activity is not in the current screen and some other activity has been started.I want to check this situation, when the activity is not the current one, I'd not send the Toast notification.