Ben H
Ben H

Reputation: 3905

Why does stopping my service kill the foreground activity?

In my app's main activity, I start a service like this:

Intent i = new Intent(this, MyService.class);
startService(i);

When the user presses a button, I stop the service like this:

Intent i = new Intent(this, MyService.class);
stopService(i);

When I do this, the foreground activity that's making the call is killed and disappears.

Thinking that maybe the service needed to run in a separate process, I tried setting android:process=":remote" on the service in the manifest, but that didn't change the behavoir.

It seems that I can't stop the service without killing the entire app. Is there a way around this?

Upvotes: 0

Views: 1109

Answers (2)

Ben H
Ben H

Reputation: 3905

I figured out why the app was being killed. In my service's onDestroy() method, I was disabling a broadcast receiver like this:

PackageManager pacman = getPackageManager();
pacman.setComponentEnabledSetting(
     new ComponentName(this, MyReceiver.class), 
     PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
     0);

This code disables a broadcast receiver that is defined in AndroidManifest.xml. The last argument, 0, should have been set to DONT_KILL_APP. I changed the code to this, and it solved the problem:

PackageManager pacman = getPackageManager();
pacman.setComponentEnabledSetting(
     new ComponentName(this, MyReceiver.class), 
     PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
     PackageManager.DONT_KILL_APP);

Upvotes: 1

Andrew White
Andrew White

Reputation: 53506

Without logs we can't know for sure but I have a good hunch that your app is throwing an exception and that you are silently catching and closing or you have an odd critical path that call finish.

In any case, what you described is not expected but we need more info to give a better answer

Upvotes: 0

Related Questions