How to restart a service to call the oncreate again?

Asked

Viewed 134 times

2

Hey, guys, let’s go I have a service and I have all his logic on Oncreate. The problem is that I need it to go through the oncreate again when I perform an interaction on my Activity.

I’ve tried using the

stopService(new Intent(this, YourService.class)); startService(new Intent(this, YourService.class));

but it didn’t work. Does anyone know how to do that?

1 answer

2

The logic of the task a Service performs should not be placed in the onCreate().
The responsibility of onCreate() is to initialize the Service, nothing more.

Thus, all this logic should be placed in the method onStartCommand(), that will be called whenever we perform startService(new Intent(this, YourService.class));.

  • Ramaral, a very intelligent man! I liked the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.