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
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
if(!activity.isFinishing() && !activity.isDestroyed())
Use the isActivity variable to check if activity is alive or not.
Then check
In addition to the accepted answer, if you have multiple instances of the activity, you can use a counter instead:
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:
That works for me with several activitys open at the same time.
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?