Where in onStart()
, onStop()
, onDestroy()
of an activity do I call super.onStart()
, super.onStop()
, super.onDestroy()
?
Asked
Active
Viewed 1.2k times
24
-
1Usually, call the base class first, before proceeding to handle anything within the onXXXX() methods within an activity. – t0mm13b Aug 12 '12 at 19:30
-
Possible duplicate of [What is the correct order of calling superclass methods in onPause, onStop and onDestroy methods? and Why?](http://stackoverflow.com/questions/18821481/what-is-the-correct-order-of-calling-superclass-methods-in-onpause-onstop-and-o) – Blaisorblade Aug 27 '16 at 12:44
2 Answers
21
That's my way of calling these super methods:
OnCreate()
: Definitely the first thing.OnDestroy()
: The last thing.OnStop()
: The last thing.
However, for the last two, no matter where you call them (in most most cases). So some people prefer to put them at the first to be consistent.
-
3Why would you not adhere to the Java's rule? My guts feeling says, that if you call super() first in onDestroy/onStop, that some (the base things) wouldn't exist anymore, and that could cause problems when "cleaning up", however, as @Tomer said, that would be against Java rules. Generally, there are good reasons to adhere to Java rules, even if I dont have insight in a particular rule. – mrd Aug 12 '12 at 21:11
-
1That's why I call `super.onStop()` ans `super.onDestroy()` at last. I want to clean up my stuff before the system clean up his. However, as I said, it won't differ who clean up first in most scenarios. Of course you can follow java rules and ignore your guts :-) – iTurki Aug 12 '12 at 22:08
5
I would call super in the begining, I would probably have the base class complete its work before I do the work of the derived, like the rule in Java

Tomer Mor
- 7,998
- 5
- 29
- 43
-
And there is good reasoning in this approach. For example, I use EventBus to 'register' and 'unregister' the subscribers in a base Activity class. Given that 'unregister' gets called in the baseclass, 'super' needs to be invoked in onStop in the derived class in the begining, so it stops receiving events and then carry on cleaning up anything else. – strangetimes Apr 17 '15 at 15:14