Is there a way to know if the activity I want to start, is already the activity on the foreground?
Asked
Active
Viewed 128 times
0
-
See that question : http://stackoverflow.com/questions/2314969/how-to-determine-if-one-of-my-activities-is-in-the-foreground – Stephane Mathis Oct 01 '12 at 14:55
-
You could have a look at launchModes. With the proper launchMode, you can prevent the device from starting more than one instance of your Activity, if that is what you want. Be careful when you use it, because in some launchmodes, onActivityResult doesn't work the way you're used to. – Christine Oct 01 '12 at 15:44
1 Answers
0
You can get the name of Activity
in foreground with ActivityManager
. Here is the code:
ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
String foreGroundActivityName = runningTaskInfos.get(0).topActivity.getClassName().toString();
You can then compare the name with your Activity
. And make sure you add android.permission.GET_TASKS
permission in manifest file.

skink
- 5,133
- 6
- 37
- 58

Deep Verma
- 792
- 1
- 6
- 12