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
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.
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.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
andonCreate()
set this variable true.Assign getters and setters of this variable.
And then from another activity or Service