views:

65

answers:

1

The first time my app loads it starts my main activity, and inside my main activity i automatically start a service:

Intent s = new Intent(this, Soc.class);
startService(s);
//start the service for the first time

I need to make sure that when the user is opening the app NEXT TIME, it kills the old service, and recreate the service:

@Override
//this code is on every activity in my application
protected void onRestart()
{
     super.onRestart();
     Intent s = new Intent(this, Soc.class);
     stopService(s);
     //kill the service
     startService(s);
     //start a brand new service
}

Is this the correct way of killing a service and renewing the service?

A: 

Is this the correct way of killing a service and renewing the service?

Well, it might work, but it is rather wasteful. Why are you bothering with a service in this case? Or, conversely, why do you have to destroy and recreate the service just to update it?

CommonsWare
because my service s a socket, and my application is a session based application. So in order to use the application the user must be connected to the server and request a new session.
aryaxt
@aryaxt: That has nothing whatsoever to do with destroying and recreating the service.
CommonsWare
Yep, that was a good idea, instead of killing the service, and creating it again, i closed the socket, and connect it to my server again.
aryaxt