0

In terms of life cycle, when I call stopService(intent), does the service's onDestroy() get called right away? I am asking because I have problem getting the onDestroy() called to kill this thread the service generates.

Thanks in advance!

button.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            Log.d(Tag, "in button's onClick");


            Log.d(Tag, field.getText().toString());
            String busNo = field.getText().toString();
            field.setText("");
            field.setAdapter(clearAdapter);
            String url = makeURL(busNo);
            Log.d(Tag, "after makeURL(), url is now "+url);
            Intent intent = new Intent(JSoupTest9Activity.this, ServiceTest.class);
            Log.d(Tag, "intent created...");
            intent.putExtra("URL", url);
            Log.d(Tag, "intent's extra put");
            Log.d(Tag, "stopping intent service");
            stopService(intent);
            Log.d(Tag, "starting intent service");

            startService(intent);
            Log.d(Tag, "service started");

            //pendingIntent = PendingIntent.getService(JSoupTest8Activity.this, 0, intent,  0);
            //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30*1000, pendingIntent);

        }
    });

As you can see, I called stopService() to kill any previous services(query threads) to start a new service(new query thread).

Jason Ching
  • 1,037
  • 4
  • 19
  • 27

1 Answers1

3

It will not be called inmediatelly only by statically calliong stopService. As pointed in the activity's lifecycle diagram, it will be destroyed if the vm needs resources

When I am in doubt, I simply use the log tool and check their output on LogCat. If you override OnDestroy method like this:

protected void onDestroy() {
Log.d("event","onDestroy was Called!");
super.onDestroy();
}

You can verify yourself when it was called.

And if you want to destroy one of your activities in Background you may use an activity manager:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("<packagename>");

If it's not within your app you of course need to set permissions on the manifest. With my limited experience, I have not found a time where I needed to destroy any process; but I do have found much documentation telling me not to, for example

Checking further, I think this quesiton was covered already

Community
  • 1
  • 1
quinestor
  • 1,432
  • 4
  • 19
  • 40