When overriding a method should I call the super implementation before or after my code?

Asked

Viewed 31 times

1

Watching and researching some tutorials on the internet, I came across some code in JAVA on ANDROID, as below:

@Override
protected void onPause() {
    sensorManager.unregisterListener(this);
    super.onPause();
}

@Override
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

The Mainactivity class is basically working with the accelerometer to collect and display the X,Y and Z axes of the device.

My question, what is the difference of being above or below the super.onPause() Sensormanager registration release?

1 answer

3


In most cases it makes no difference, it is best to always read the documentation of the method.

The documentation of onPause() only refers to the obligation to call, does not say when.

If in doubt, it seems logical to me that,

  • In methods that intervene in the initialization, such as onCreate(), onStart(), onRestart() and onResume(), first call the super method.

  • In the methods involved in completion, such as onPause(), onStop() and onDestroy(), call the super method at the end.

  • thank you very much for the explanation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.