In one Activity, I define the following Button listener:
final Button button = (Button) findViewById(R.id.buttonExit);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
stopService(new Intent(StartupActivity.this, SamcomService.class));
finish();
}
});
As you can see, the button is supposed to stop the running Service (created in a previous step), and then finish itself.
When I press the button, the Service.onDestroy is executed just as expected. In the onDestroy I do some cleaning, and then lastly call super.onDestroy():
@Override
public void onDestroy() { // the service onDestroy
// Do some cleaning
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1);
// more cleaning
Toast.makeText(this, "The service has been stopped! Wii!", Toast.LENGTH_LONG).show();
super.onDestroy();
}
Im my world, that means this Service is dead and buried, along with all the variables in it. Right? Well, it doesnt seem like it.
The think is, I have a String in my Service that I append text to before I click the button to stop the service. Something like this:
public class SamcomService extends Service {
private String startupText = "";
private void addTextToStartup(String text)
{
startupText += text;
// Sending a broadcast, not relevant
// ...
}
// ...
}
That string, startupText, is not reset when I launch my app again! Its like the Service wasn't killed at all. The startupText contains all the text that was added to it in the previous run.
Why is that? What am I missing? Isnt the Service dead? When I launch the app again, the Service onCreate method is called, implying that it is started from scratch...
--- EDIT ---
I just read this: What exactly does onDestroy() destroy?
That means that the onDestroy doesnt really destroy anything. Correct? Its pretty lame, and extremely annoying. One well-visited thread here on SO that I create almsot 2 years ago discussing this issue I guess...: Is quitting an application frowned upon?