I am trying to figure out how Android works when it comes to lifecycle issues.
I have decided to have a long-running Service to hold my TCP-connections and other stuff.
I have one Activity, StartupActivity. That Activity starts a service, and then I press a button to Finish the Activity. I then launch the same app/Activity again, and thus the startService is executed again.
However, I expected the Service to still be alive (there has been no onDestroy called), but the onCreate-method in the Service is still being executed.
Why?
STEP 1: onCreate in StartupActivit is executed:
@Override
protected void onCreate(android.os.Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.startup);
startService(new Intent(StartupActivity.this, SamcomService.class));
registerReceiver(connectionReceiver, new IntentFilter("STARTUP_PROGRESS_MESSAGE"));
registerReceiver(connectionReceiver, new IntentFilter("STARTUP_FINISH"));
final Button button = (Button) findViewById(R.id.buttonExit);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
STEP 2: I press the button, and Activity.finish() is called (I am returned to the home screen)
STEP 3: I launch the app again, and the onCreate for the Activity is once more executed, thus starting the same Service
Now, when I start the app the second time the Service should be running (how do I check that?). I havent seen the Toast that is displayed when the Service onDestroy is called, and I also see the Notification that the Service creates (and removes onDestroy).
However, the onCreate is executed a second time. Do I now have more than one Service running at the same time?