Is an Android Service Guaranteed to Call Ondestroy()

Is an android service guaranteed to call onDestroy()?

I'm not sure where you're seeing that a Service is guaranteed to have onDestroy() called. As far as I know, this isn't the case. If you read this page of the docs, it describes the conditions in which a service could be killed. So if you're asking if a process which hosts both an activity and service is being killed, will onDestroy() be called on the service (but not on the activity) then the answer is no; a service's onDestroy() will not necessarily be called. As to whether a service-only process can be abruptly killed by the OS: yes, it can. This is especially true when you have a lot of work to do, and your onStartCommand call only queues up the work to do asynchronously. Then the service will be spending the majority of its time not in the protected onCreate, onStartCommand or onDestroy methods.

Is onDestroy called in an application when the phone runs out of battery?

In reality there is no telling what will happen. There's even doubt about if onDestroy() will be called under normal circumstances.

One of the few situations where onDestroy() in an Activity SHOULD be called is when using the BACK button or if an event in the Activity explicitly results in calling finish(). In result, and in theory, the Activity will be stopped (onStop() is called) then destroyed (onDestroy() is called).

The ambiguity WRT the clean shutdown of app components comes when processes are actually 'killed' - this is why Task Killers are so evil as they basically forcibly rip the process out of memory regardless of running state and usually prevent any cleanup operations to occur.

In an absolute emergency (such as when the battery is at absolute minimum), the system will do its level best to shut down any running processes as cleanly as possible but there's no guarantee it will do this successfully.

In particular, if you have any 'mission critical' data or state which needs to be saved then do it when an Activity is paused (i.e., in onPause() or at the very least when it is stopped (in onStop()).

I personally rarely make use of onDestroy() for the reasons I've outlined above - in general I work on the creation -> start -> resume -> pause -> stop - restart life-cycle methods.

Finally, you have to remember your app may have a number of different components (Activities, Services and either an explicit or implicit Application) - in the case of a low battery shutdown, each of these components may be handled differently. All in all, however, if the process is 'ripped' ot of memory and forcibly stopped, there's no guarantee of what you'll find once you've re-charged the battery and restarted your app unless you plan for saving data and / or state.

Why implement onDestroy() if it is not guaranteed to be called?

onDestroy will be called if you explicitly call finish(); yourself.

Your main activity calls startActivityForResult on a map activity.

Map activity with a LocationListener, the user clicks the map and selects say a local restaurant.

The activity then , sets up some extras to be sent back to your main activity, it then explicitly call's finish(); on itself and in the onDestroy kills the LocationListener and other variables you had invoked.

Just found this in the docs

onDestroy() = The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Android: stopService doesn't call onDestroy

First, it's onDestroy, not OnDestroy . Second, you must use the @Override annotation for compile-time checking, so your Service code should look somewhat like this:

@Override
public void onDestroy(){
Log.v("SERVICE","Service killed");
player.stop();
super.onDestroy();
}

Android: OnDestroy isn't called when I close the app from the recent apps button

As specified in the Android documentation, it is not guaranteed that onDestroy() will be called when exiting your application.

"There are situations where the system will simply kill the activity's hosting process without calling this method"

https://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29

Instead, you can create a service which will be notified when the Task your activities are running inside is destroyed.

Create the service class:

public class ClosingService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);

// Handle application closing
fireClosingNotification();

// Destroy the service
stopSelf();
}
}

Declare / register your service in the manifest (within the application tag, but outside any activity tags):

<service android:name=".services.ClosingService"
android:stopWithTask="false"/>

Specifying stopWithTask="false" will cause the onTaskRemoved() method to be triggered in your service when the task is removed from the Process.

Here you can run your closing application logic, before calling stopSelf() to destroy the Service.



Related Topics



Leave a reply



Submit