bre_dev
bre_dev

Reputation: 572

Why my Android Service is killed when I launch an Activity from it?

I have a Service that has to launch an external Activity. I would like that even after have launched the Activity, that Service will continue to be running. The problem is that the onDestroy() is invoked and the service is killed. here it is my code:

How I start the external Activity from inside the Service:

      Intent intent = new Intent(Intent.ACTION_DIAL);        
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent);

Any idea?

Upvotes: 1

Views: 159

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007554

Is it normal that when I start an activity from inside a Service, that Service is being killed?

Services are not "killed".

Your service will run until:

  • You call stopService() referencing the service, or
  • The service calls stopSelf(), or
  • If you are using the binding pattern, all bindings (via bindService()) are unbound (via unbindService()), or
  • Android terminates your process, such as due to low memory conditions or by user request

Hence, you need to determine which of the conditions is true. None necessarily have anything to do with starting an activity.

Note that starting an activity from a service is generally not a good idea, particularly if this may occur at arbitrary times rather than based on user input. Users dislike activities appearing out of nowhere.

Upvotes: 2

Related Questions