Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way of calling super.onStop() when unregistering a listener

Whats the correct way of calling super.onStop(), i.e, when unregistering a listener?

I have seen:

protected void onStop() {
  sensorManager.unregisterListener(this);
  super.onStop();
}

OR

protected void onStop() {
  super.onStop();  
  sensorManager.unregisterListener(this);
}
like image 303
Enrique Avatar asked Jun 25 '11 23:06

Enrique


Video Answer


2 Answers

You should always call it first, mostly as a protection mechanism: if there is an exception then the superclass instance method will already have been called.

like image 168
Femi Avatar answered Sep 19 '22 15:09

Femi


It doesn't matter. Unless you are dependent on some state continuing to be initialized (and as far as any framework classes are concerned I can guarantee you aren't) you can freely call it after the superclass. If the superclass throws an exception your whole app is going to crash, so there is no reason to order one way or the other due to that.

That said, just for consistency putting these calls on the first line is nice because that is where people expect to see them and it will help avoid you making mistakes in the future such as deleting code in the method and accidentally deleting the call to the super class.

like image 25
hackbod Avatar answered Sep 18 '22 15:09

hackbod