开发者

What happens if I startService from onCreate in Activity, finish() the Activity and then start the app again?

开发者 https://www.devze.com 2023-04-12 08:17 出处:网络
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 an开发者_开发技巧d other stuff.

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 an开发者_开发技巧d 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?


However, the onCreate is executed a second time. Do I now have more than one Service running at the same time?

No, and I know this is confusing, but the startService() method does not start the service. It will be called in the service everytime you run it. For instance, if the service isn't started, it will start it and then run it. If the service is started, it will just run it. The android documentation says it was designed this way so that the startService() method is the easiest way to communicate with your service.

STEP 2: I press the button, and Activity.finish() is called (I am returned to the home screen)

This depends on if your service is running on the same process as your activity or not. If you have :process = "something" in your service declaration in the manifest, and you did everything else perfectly, the service is in another process and there are only three situiations inwhich your service will be destroyed. 1, your activity is destroyed(Almost guaranteed not to happen as your process has its activities active priority), 2, There is EXTREMELY low memory, 3 the service calls selfStop().

Since finish() does not destroy your app(Android keeps it around in memory encase the user comes back to it), your service is either just paused(If it was in the same process) or still running(If it was in a different process). Regardless another call to startService() will restart it if it was paused and run the service's startService method. You only ever have one of your services around.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号