1

I'm using job scheduler and my main activity schedules a service to start the next time there is internet connection. So when the app is first installed, the layout has just one button, when you click it, it schedules the network dependent service and then I want the activity to be finished(as in the layout to be destroyed so the user gets the screen he had before the app was installed) so I have 2 questions about that:

1) if the activity that schedules the service is destroyed does that mean that the service is still scheduled or is it lost ?

2) why does calling the method finish() at the end of the onClick event (after scheduling the service) not finish the activity and instead nothing happens as if finish() doesn't even exist ?

This is the OnCreate code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);

            JobInfo.Builder builder = new JobInfo.Builder(1,
                new ComponentName(getPackageName(),
                    JobSchedulerService.class.getName()));

            builder.setPersisted(true);

            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);

            mJobScheduler.schedule(builder.build());
        }
    });
}
Krylez
  • 17,414
  • 4
  • 32
  • 41
student93
  • 307
  • 2
  • 12

1 Answers1

0

1) if the activity that schedules the service is destroyed does that mean that the service is still scheduled or is it lost?

No, JobScheduler doesn't need your activity or app to keep alive in order to work.

2) why does calling the method finish() ... not finish the activity ...?

Double check this behavior. Keep in mind that an attached, paused debugger will stop execution and prevent the activity from finishing. The behavior of this method is a bit nuanced.

Community
  • 1
  • 1
Krylez
  • 17,414
  • 4
  • 32
  • 41