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
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.
ActivityLifecycleCallbacks is a great way of keeping track of all the activities in App:
ActivityState:
Extend the Application class and provide its reference in Android Manifest file:
Ckeck anywhere from Activity for status of other activity:
https://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html
An option without using any auxiliar variable is:
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.
I think more clear like that:
I used MyActivity.class and getCanonicalName method and I got answer.
Found an easy workaround with the following code