176

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
Undo
  • 25,519
  • 37
  • 106
  • 129
user560571
  • 1,977
  • 3
  • 17
  • 17
  • To check whether an Activity is running or not you can go through this answer: http://stackoverflow.com/a/39079627/4531507 – Rahul Sharma Aug 22 '16 at 12:48

28 Answers28

256

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.

Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
siliconeagle
  • 7,379
  • 3
  • 29
  • 41
  • 6
    Some one pointed this out to me that..Sharedpreference should be preferred over a static variable due to memory leak problems. – Ayush Goyal Apr 17 '13 at 19:58
  • 13
    What if there are different activities of the same class running? What if you extend `MyActivity` with `MyChildactivity` and want to check if the child is active? – Mister Smith Sep 02 '13 at 10:27
  • 2
    Depending on your definition of "running" you might want to change the state of the variable in onResume and onPause.... – G. Blake Meike Aug 15 '14 at 20:51
  • 2
    This will fail miserably if multiple instances of the activity are running. You could use an ordered broadcast instead. Inside the activity itself (on instance level) use a mIsRunning boolean to get this state – nickmartens1980 Aug 20 '14 at 10:36
  • What happens if activity is force closed? will onStop() called and reset the active variable? – VSB Oct 03 '14 at 22:47
  • No it won't. But the jvn will restart hence the variable will be reset to default as the life cycle starts again. – siliconeagle Oct 04 '14 at 23:58
  • Nice one. Fresh & clean looking code. (I just have to set isActive to false in onDestroy()) – Martin Pfeffer Nov 29 '14 at 03:52
  • 2
    Instead of using a boolean, use an counterint. in onstart, increment it, in destroy decrement it, create a method to check if counter > 0 – James Alvarez Apr 02 '15 at 13:02
  • 5
    This solution is not a good solution at all. Lets say your Activity calls a Fragment, for example, the fragment will be over the Activity yet the Activity will not call onPause, and if you close the fragment onStop, onStart, or any other lifecycle methods will not be called either. Best solution is to check in your Application class the visibility as described here: http://stackoverflow.com/questions/18038399/how-to-check-if-activity-is-in-foreground-or-in-visible-background – portfoliobuilder Jun 26 '15 at 23:32
  • if you extend some Activity class then define boolean static fields in child class not in parent class, then it is not possible to make mistakes, and perhaps you will need to check for instances about you children activity classes not for you base activity class – Stoycho Andreev Aug 27 '15 at 20:21
  • 8
    If you recommend statics, you get a -1 from me – Matei Suica Feb 11 '16 at 17:23
  • 1
    Why should it be static at all? It could just be non-static boolean isActive. Then isActive is always bound to that instance of the Activity. I can't see why static should be involved. – JohnyTex Feb 17 '16 at 14:29
  • the question was for checking externally from a service it's very old. – siliconeagle Feb 18 '16 at 15:39
  • OMG! Just use an event bus or something for this! http://blog.kaush.co/2014/12/24/implementing-an-event-bus-with-rxjava-rxbus/ – Boy Dec 21 '16 at 12:39
  • and what happen if the acticity crash ? i guess the onStop will never be call ? – zeus Apr 21 '17 at 23:42
  • 1
    For those who downvote, this answer might not help in all cases but did help in some case, e.g. you sure this activity only start single instance on the same time. You shouldn't downvote simply because it doesn't cover ALL cases. – 林果皞 Feb 02 '18 at 11:05
  • onstop and ondestroy might not be guaranteed to be called, see https://stackoverflow.com/questions/29355290/onstop-vs-ondestroy#comments-29356415. – CamHart Apr 27 '18 at 05:46
  • 1
    How about onResume() and onPause() ? – Carlos López Marí Jun 15 '18 at 13:02
  • I guess static is bad option to use here, and we should also take care of call back onstop(), there may be some corner cases where this can fail. Why not use activity.isFinishing() method. – Saad Bilal Jan 08 '19 at 18:13
  • This is essentially implemented for you, but better, with the native [`Activity#isDestroyed` method](https://developer.android.com/reference/android/app/Activity.html#isDestroyed()) – yuval Sep 25 '19 at 21:24
  • 1
    Not only this does not properly register all activity states but it cannot work for more than one activity. This solution should not be here – Julius Dec 10 '19 at 13:52
102

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(Lifecycle.State.STARTED)

Activity is in the foreground

getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)
vovahost
  • 34,185
  • 17
  • 113
  • 116
malinjir
  • 1,475
  • 2
  • 12
  • 17
47

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;
    }
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
Xenione
  • 2,174
  • 1
  • 23
  • 30
  • 2
    I try to avoid making temporary variables before a 'for' loop; for (RunningTaskInfo task: ActivityManager.getRunningTasks(Integer.MAX_VALUE)) { ... – mikebabcock May 31 '12 at 21:26
  • How can I call this function? – Behzad Nov 11 '12 at 23:07
  • 1
    as usual, is a methode that you can call inside your class as a 'function' do you need an example? – Xenione Nov 12 '12 at 11:41
  • 10
    From http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks%28int%29 "This should never be used for core logic in an application, such as deciding between different behaviors based on the information found here. Such uses are not supported, and will likely break in the future." – joe_deniable Apr 13 '14 at 08:29
  • 18
    As of API level 21 (Android 5.0 Lollipop) this method has been deprecated. – AxeEffect Nov 11 '14 at 03:48
  • Bad practice. use this instead getWindow().getDecorView().getRootView().isShown() – ILYAS_Kerbal Aug 25 '17 at 14:43
  • This answer originally used `getRunningTasks` which was deprecated in API level 21. Now, it uses `getRunningTasks`, which is not deprecated, currently we are at API level 30. – Ben Butterworth Sep 10 '21 at 15:42
37

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.

Fely
  • 38
  • 8
GaRRaPeTa
  • 5,459
  • 4
  • 37
  • 61
25

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;
}
mahyar
  • 363
  • 3
  • 8
  • 2
    As previously mentioned, it might be not a good idea to use `getRunningTasks()` as it was deprecated: http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/app/ActivityManager.java#1991 – Vitalii Dmitriev Jan 15 '19 at 16:22
21

Far better way than using a static variable and following OOP

Shared Preferences can be used to share variables with other activities and services from one application

    public class example extends Activity {

    @Override
    protected void onStart() {
        super.onStart();

        // Store our shared preference
        SharedPreferences sp = getSharedPreferences("OURINFO", MODE_PRIVATE);
        Editor ed = sp.edit();
        ed.putBoolean("active", true);
        ed.commit();
    }

    @Override
    protected void onStop() {
        super.onStop();

        // Store our shared preference
        SharedPreferences sp = getSharedPreferences("OURINFO", MODE_PRIVATE);
        Editor ed = sp.edit();
        ed.putBoolean("active", false);
        ed.commit();

    }
}

Use shared preferences. It has the most reliable state information, less application switch/destroy issues, saves us to ask for yet another permission and it gives us more control to decide when our activity is actually the topmost. see details here abd here also

rpattabi
  • 9,984
  • 5
  • 45
  • 53
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
12
if(!activity.isFinishing() && !activity.isDestroyed())

From the official docs:

Activity#isFinishing()

Check to see whether this activity is in the process of finishing, either because you called finish() on it or someone else has requested that it finished. This is often used in onPause() to determine whether the activity is simply pausing or completely finishing.

Activity#isDestroyed()

Returns true if the final onDestroy() call has been made on the Activity, so this instance is now dead.

yuval
  • 6,369
  • 3
  • 32
  • 44
Codelicious
  • 604
  • 10
  • 12
9

This is code for checking whether a particular service is running. I'm fairly sure it can work for an activity too as long as you change getRunningServices with getRunningAppProcesses() or getRunningTasks(). Have a look here http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses()

Change Constants.PACKAGE and Constants.BACKGROUND_SERVICE_CLASS accordingly

    public static boolean isServiceRunning(Context context) {

    Log.i(TAG, "Checking if service is running");

    ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

    List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

    boolean isServiceFound = false;

    for (int i = 0; i < services.size(); i++) {

        if (Constants.PACKAGE.equals(services.get(i).service.getPackageName())){

            if (Constants.BACKGROUND_SERVICE_CLASS.equals(services.get(i).service.getClassName())){
                isServiceFound = true;
            }
        }
    }

    Log.i(TAG, "Service was" + (isServiceFound ? "" : " not") + " running");

    return isServiceFound;

}
kkudi
  • 1,625
  • 4
  • 25
  • 47
6

thanks kkudi! I was able to adapt your answer to work for an activity... here's what worked in my app..

public boolean isServiceRunning() { 

ActivityManager activityManager = (ActivityManager)Monitor.this.getSystemService (Context.ACTIVITY_SERVICE); 
    List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE); 
    isServiceFound = false; 
    for (int i = 0; i < services.size(); i++) { 
        if (services.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{com.lyo.AutoMessage/com.lyo.AutoMessage.TextLogList}")) {
            isServiceFound = true;
        }
    } 
    return isServiceFound; 
} 

this example will give you a true or false if the topActivity matches what the user is doing. So if the activity your checking for is not being displayed (i.e. is onPause) then you won't get a match. Also, to do this you need to add the permission to your manifest..

<uses-permission  android:name="android.permission.GET_TASKS"/>

I hope this was helpful!

alyon2002
  • 265
  • 4
  • 7
  • 1
    This uses-permission was deprecated in API level 21. http://developer.android.com/reference/android/Manifest.permission.html#GET_TASKS – Guy West Feb 24 '16 at 13:03
  • 2
    Downvote the annswer becuase to access top activity(services[i].topActivity) we min API level Q .Below that wont work. – Debasish Ghosh Jan 28 '20 at 10:58
5

There is a much easier way than everything above and this approach does not require the use of android.permission.GET_TASKS in the manifest, or have the issue of race conditions or memory leaks pointed out in the accepted answer.

  1. Make a STATIC variable in the main Activity. Static allows other activities to receive the data from another activity. onPause() set this variable false, onResume and onCreate() set this variable true.

    private static boolean mainActivityIsOpen;
    
  2. Assign getters and setters of this variable.

    public static boolean mainActivityIsOpen() {
        return mainActivityIsOpen;
    }
    
    public static void mainActivityIsOpen(boolean mainActivityIsOpen) {
        DayView.mainActivityIsOpen = mainActivityIsOpen;
    }
    
  3. And then from another activity or Service

    if (MainActivity.mainActivityIsOpen() == false)
    {
                    //do something
    }
    else if(MainActivity.mainActivityIsOpen() == true)
    {//or just else. . . ( or else if, does't matter)
            //do something
    }
    
Pang
  • 9,564
  • 146
  • 81
  • 122
coolcool1994
  • 3,704
  • 4
  • 39
  • 43
  • 3
    Are you saying that using accessor methods is better than using raw public static variables? – IgorGanapolsky Feb 04 '13 at 23:58
  • 1
    In Java it is better to use setters and getters to keep your variables private. However, I believe that in Android it is common to access public variables directly... – Stephen Apr 11 '13 at 12:36
  • 11
    It makes no sense to have a public setter since activity state should be handled only by the activity itself. You should stick with java naming conventions: isActivityOpen would be a correct getter method. Also using if boolean == true is redundant. Besides that, delegating state management to the activity is the best approach. – Lisandro May 28 '13 at 12:31
  • 8
    this is why u should have attended your courses more diligently @coolcool ;) – Jerec TheSith Aug 27 '13 at 13:45
  • 1
    And what if you have multiple instances of the activity running? – nickmartens1980 Aug 20 '14 at 10:34
  • This checks for the activity being open, not whether it is running or not. – Zackline Feb 19 '16 at 11:50
4

I think the accepted answer is an awful way of handling this.

I don't know what the use case is, but please consider a protected method in the base class

@protected
void doSomething() {
}

and override it in the derived class.

When the event occurs, just call this method in the base class. The correct 'active' class will handle it then. The class itself can then check if it is not Paused().

Better yet, use an event bus like GreenRobot's, Square's, but that one is deprecated and suggests using RxJava

Boy
  • 7,010
  • 4
  • 54
  • 68
4

ActivityLifecycleCallbacks is a great way of keeping track of all the activities in App:

public class BaseActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

private ActivityState homeState, contentState;

@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
    if (activity instanceof HomeActivityv2) {
        homeState = ActivityState.CREATED;
    } else if (activity instanceof ContentDisplayActivity) {
        contentState = ActivityState.CREATED;
    }
}

@Override
public void onActivityStarted(Activity activity) {
    if (activity instanceof HomeActivityv2) {
        homeState = ActivityState.STARTED;
    } else if (activity instanceof ContentDisplayActivity) {
        contentState = ActivityState.STARTED;
    }
}

@Override
public void onActivityResumed(Activity activity) {
    if (activity instanceof HomeActivityv2) {
        homeState = ActivityState.RESUMED;
    } else if (activity instanceof ContentDisplayActivity) {
        contentState = ActivityState.RESUMED;
    }
}

@Override
public void onActivityPaused(Activity activity) {
    if (activity instanceof HomeActivityv2) {
        homeState = ActivityState.PAUSED;
    } else if (activity instanceof ContentDisplayActivity) {
        contentState = ActivityState.PAUSED;
    }
}

@Override
public void onActivityStopped(Activity activity) {
    if (activity instanceof HomeActivityv2) {
        homeState = ActivityState.STOPPED;
    } else if (activity instanceof ContentDisplayActivity) {
        contentState = ActivityState.STOPPED;
    }
}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}

@Override
public void onActivityDestroyed(Activity activity) {
    if (activity instanceof HomeActivityv2) {
        homeState = ActivityState.DESTROYED;
    } else if (activity instanceof ContentDisplayActivity) {
        contentState = ActivityState.DESTROYED;
    }
}

public ActivityState getHomeState() {
    return homeState;
}

public ActivityState getContentState() {
    return contentState;
}
}

ActivityState:

public enum ActivityState {
    CREATED, STARTED, RESUMED, PAUSED, STOPPED, DESTROYED;
}

Extend the Application class and provide its reference in Android Manifest file:

import android.app.Application;

public final class BaseApplication extends Application {
private BaseActivityLifecycleCallbacks baseALC;

@Override
public void onCreate() {
    super.onCreate();
    baseALC = new BaseActivityLifecycleCallbacks();
    this.registerActivityLifecycleCallbacks(baseALC);

}

public BaseActivityLifecycleCallbacks getBaseALC() {
    return baseALC;
}
}

Ckeck anywhere from Activity for status of other activity:

private void checkAndLaunchHomeScreen() {
    Application application = getApplication();
    if (application instanceof BaseApplication) {
        BaseApplication baseApplication = (BaseApplication) application;
        if (baseApplication.getBaseALC().getHomeState() == null || baseApplication.getBaseALC().getHomeState() == ActivityState.DESTROYED) {
            //Do anything you want
        }
    }
}

https://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html

Vitalii Dmitriev
  • 739
  • 1
  • 8
  • 18
4

Have you tried..

    if (getActivity() instanceof NameOfYourActivity){
        //Do something
    }
E.Mathew
  • 43
  • 5
2

I used a check if (!a.isFinishing()) and it seems to do what i need. a is the activity instance. Is this incorrect? Why didn't anyone try this?

lxknvlk
  • 2,744
  • 1
  • 27
  • 32
2

what about activity.isFinishing()

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
Ambareesh B
  • 499
  • 1
  • 5
  • 14
1

Not sure it is a "proper" way to "do things".
If there's no API way to resolve the (or a) question than you should think a little, maybe you're doing something wrong and read more docs instead etc.
(As I understood static variables is a commonly wrong way in android. Of cause it could work, but there definitely will be cases when it wont work[for example, in production, on million devices]).
Exactly in your case I suggest to think why do you need to know if another activity is alive?.. you can start another activity for result to get its functionality. Or you can derive the class to obtain its functionality and so on.
Best Regards.

user2399321
  • 605
  • 7
  • 13
1

If you are interested in the lifecycle state of the specific instance of the activity, siliconeagle's solution looks correct except that the new "active" variable should be an instance variable, rather than static.

1

Use an ordered broadcast. See http://android-developers.blogspot.nl/2011/01/processing-ordered-broadcasts.html

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. The service can check the result data to see if the broadcast was handled, and if not take appropriate action.

nickmartens1980
  • 1,593
  • 13
  • 23
1

In addition to the accepted answer, if you have multiple instances of the activity, you can use a counter instead to handle multiple instances :

class MyActivity extends Activity {

     static int activeInstances = 0;

     static boolean isActive() {
        return (activeInstances > 0);
     }

      @Override
      public void onStart() {
         super.onStart();
         activeInstances++;
      } 

      @Override
      public void onStop() {
         super.onStop();
         activeInstances--;
      }
}
Community
  • 1
  • 1
James Alvarez
  • 7,159
  • 6
  • 31
  • 46
1
public static boolean isActivityActive(Activity activity) {
   if (null != activity)
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
         return !activity.isFinishing() && !activity.isDestroyed();
      else return !activity.isFinishing();
   return false;
}
Ahamadullah Saikat
  • 4,437
  • 42
  • 39
1

I have used task.topActivity instead of task.baseActivity and it works fine for me.

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

    for (ActivityManager.RunningTaskInfo task : tasks) {
        if (task.topActivity.getClassName().equals(NotificationsActivity.class.getCanonicalName()))
            return true;
    }

    return false;
}
hassan mirza
  • 139
  • 2
  • 8
0

Found an easy workaround with the following code

@Override 
protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
                // Activity is being brought to front and not being  created again, 
                // Thus finishing this activity will bring the last viewed activity to foreground
                finish(); 
            } 
    }
Varun Bhatia
  • 4,326
  • 32
  • 46
0

Use the isActivity variable to check if activity is alive or not.

private boolean activityState = true;

 @Override
protected void onDestroy() {
    super.onDestroy();
    activityState = false;
}

Then check

if(activityState){
//add your code
}
Faran Tariq
  • 153
  • 1
  • 8
0

If you want to check if the activity is in the back stack just follow next steps. 1. Declared an ArrayList in your Application class [Application class is defined in your mainfest file in application tag]

private ArrayList<Class> runningActivities = new ArrayList<>();
  1. And add the following public methods to access and modify this list.

    public void addActivityToRunningActivityies (Class cls) {
    if (!runningActivities.contains(cls)) runningActivities.add(cls);
    }
    
    public void removeActivityFromRunningActivities (Class cls) {
    if (runningActivities.contains(cls)) runningActivities.remove(cls);
    }
    
    public boolean isActivityInBackStack (Class cls) {
    return runningActivities.contains(cls);
    }
    
  2. In your BaseActivity, where all activities extend it, override onCreate and onDestroy methods so you can add and remove activities from back stack as the following.

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    ((MyApplicationClass)getApplication()).addActivityToRunningActivityies
    (this.getClass());
    }
    
    @Override
    protected void onDestroy() {
    super.onDestroy();
    
    ((MyApplicationClass)getApplication()).removeActivityFromRunningActivities
    (this.getClass());
    }
    
  3. Finally if you want to check whether the activity is in the back stack or not just call this function isActivityInBackStack.

Ex: I want to check if the HomeActivityis in the back stack or not:

if (((MyApplicationClass) 
getApplication()).isActivityInBackStack(HomeActivity.class)) {
       // Activity is in the back stack
    } else {
       // Activity is not in the back stack
    }
Hagar Magdy
  • 293
  • 2
  • 10
0

I know this question is old and has a lot of varying answers, with various bonuses and drawbacks. My take on it, is why not roll your own IPC implementation.

class IPC {

    companion object {
        private val appContext : Context by lazy { /*genericApplicationContext*/ }

        fun initIPC(process: String){
            var file : File? = null
            file = File(appContext.cacheDir.absolutePath + "/$process")
            var output : OutputStream? = null
            try {
                output = FileOutputStream(file!!)
                output.write(0)
            } finally {
                output?.close()
            }
        }

        fun destroyIPC(process: String){
            var file : File? = null
            file = File(appContext.cacheDir.absolutePath + "/$process")
            file.delete()
        }

        fun checkForIPC(process: String) : Boolean {
            var file : File? = null
            file = File(appContext.cacheDir.absolutePath + "/$process")
            if(file.exists()) return true
            return false
        }
    }

}

This allows you to create the file before launching the activity, and then close out the "process/file" when you close the launched activity. This allows you to check in a background thread or current activity if your "process activity" is in the background to see if the file is still open signaling that the activity is alive. In my case I am calling an external API in succession but need to rate limit the calls, so use this to make sure only one activity is alive calling the APIs at a time.

0

This is what I came up with for helping keep track of different activities with their classes derived from a base 'helper' class.

protected static Dictionary<string, bool> _activityInstances = new Dictionary<string, bool>();
protected static Dictionary<string, bool> _activitiesVisible = new Dictionary<string, bool>();
        
protected override void OnStart()
{
    base.OnStart();
    _activityInstances[this.GetType().Name] = true;
}

protected override void OnDestroy()
{
    _activityInstances[this.GetType().Name] = false;
    base.OnDestroy();
}

protected override void OnResume()
{
    base.OnResume();
    _activitiesVisible[this.GetType().Name] = true;
}

protected override void OnPause()
{
    _activitiesVisible[this.GetType().Name] = false;
    base.OnPause();
}

public static bool activityIsInstanced(string type)
{
    return _activityInstances.ContainsKey(type) ? _activityInstances[type] : false;
}

public static bool activityIsVisible(string type)
{
    return _activitiesVisible.ContainsKey(type) ? _activityInstances[type] : false;
}

It is able to inherited but in order to run a test (i.e. before launching a new copy of a particular activity) you would need to call the static method with the name of the class, e.g.

    if (!SettingsEdit.activityIsInstanced("SettingsEdit"))
    {
        Intent intent = new Intent(this, typeof(SettingsEdit));
        Bundle bundle = new Bundle();
        bundle.PutString(MY_SETTING, this.someSetting);
        intent.PutExtras(bundle);
        this.StartActivityForResult(intent, 0);
    }

If it helps anyone, I thought I'd share it here.

0

just Use it

private fun isActivityRunning(context: Context, activityClass: Class<out Activity>): Boolean {
    val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    val taskInfoList = activityManager.getRunningTasks(Int.MAX_VALUE)
    for (taskInfo in taskInfoList) {
        val componentName = taskInfo.baseActivity
        if (componentName != null && componentName.className == activityClass.name) {
            return true // Found the activity in the stack
        }
    }
    return false // Activity not found in the stack
}
Hossein Kurd
  • 3,184
  • 3
  • 41
  • 71
-1

This work if you don't have the same activity in foreground. If you open from notification don't work i made some adjustments and came with this:

public static boolean ativo = false;
public static int counter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    counter++;
}

@Override
protected void onStart() {
    super.onStart();
    ativo = true;
}

@Override
protected void onStop() {
    super.onStop();
    if (counter==1) ativo = false;
}

@Override
protected void onDestroy() {
    counter--;
    super.onDestroy();
}

That works for me with several activitys open at the same time.