Reputation: 301
How can i check if my android app is already running to prevent double launch?
How can i make "hard exit" to prevent my app running on background?
Upvotes: 0
Views: 3648
Reputation: 50538
It is not possible to have "double launch". If you application is running already, if you try to launch another instance, than you'll resume the first launch instance.
You can finish activity by adding .finish()
in all scenarios when application can be in onPaused()
sequence of the lifecycle or add finish()
in onPaused()
Upvotes: 1
Reputation: 33996
This may help you
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);
if((serviceList.size() > 0)) {
boolean found = false;
for(int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
//Your service or activity is running
found = true;
break;
}
}
if(found) {
//Close your app or service
}
}
Upvotes: 0