3

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?

Community
  • 1
  • 1
Ted
  • 19,727
  • 35
  • 96
  • 154
  • 1
    You should add an onCreate method to your service that initializes the string. After onDestroy, android may keep the process and its memory alive (although stopped) for faster creation. – Laurent' Oct 12 '11 at 20:32
  • This is what bugs me, and has always bugged me since I started looking at Android. Its so error-prone to not be able to "wipe everything", a clean restart so that you know that you are really starting from scratch. This way, its so easy to miss something instead of just doing the extremely reliable restart. – Ted Oct 12 '11 at 20:35
  • Android first law is 'thou may respect Activity lifecycle'. After this I do not feel it's that error prone ;) Did it solve your problem? – Laurent' Oct 13 '11 at 05:18
  • 1
    @Laurent' : I actually used the System.exit(0) in my onDestroy-method in my Service. That took care of the problem =) – Ted Oct 18 '11 at 22:43

1 Answers1

3

So, as I wrote in my EDIT above:

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...: Quitting an application - is that frowned upon?

However, I solved my problem a way that of course everyone will say "noooo!" to. However, it works nicely. I simply put System.exit(0) in my onDestroy-method in the Service. No more lingering variables =)

Community
  • 1
  • 1
Ted
  • 19,727
  • 35
  • 96
  • 154