Difference between code before and after super()
Asked Answered
A

3

50

Look at the sample codes below

@Override
protected void onPause() {
    ...some code here...
    super.onPause();
}

and

@Override
protected void onPause() {
    super.onPause();
    ...some code here...
}

When I asked about differences in code, I did not mean about the flow of execution, which is abvious.

So what is the real difference between these codes? When is it advised to use your code before super() call, and when to use your code after super() call? I guess there are situations when this does matter.

Alexei answered 25/8, 2011 at 13:22 Comment(2)
@Tim: This is only true in constructorsJointress
Yep, just tested it out. You are correct, I was getting the two mixed up.Teevens
D
47

You should not place any of your code before super.onPause(), cause this method lets the system do what it needs to do to properly pause your application. Any code you want to execute in the onPause() callback should be placed after the call to super.onPause(). Hope this helps.

Quote from Activities:

Note: Your implementation of these lifecycle methods must always call the superclass implementation before doing any work, as shown in the examples above.

Durrace answered 25/8, 2011 at 13:27 Comment(3)
yeah i think you will find that calling the super first follows OO pragma as well, as you would want to do inherited class stuff first before doing unique stuff.Myology
What about Log? We follow that pattern except for logging, i.e. We always have a Log.d trace just before the super call.Freud
I prefer LIFO for Android and onPause being an outgoing event I call super.onPause lastKlara
E
27

Had to chip in with this link to a thread with an identical subject:

super.onResume() at beginning or end of method? Does it matter?

Note the quote from Dianne Hackborn

Yeah this is a good pattern. In most cases it probably doesn't matter, but it's a general rule: during any kind of initialization, let the super class do their work first; during any kind of finalization, you do your work first.

Eanore answered 25/8, 2011 at 14:3 Comment(2)
I think that Dianne's quote can be seen as a rule. After all, she's a member of Android engineers' team, right?!Alexei
That's right. It seems we have conflicting advice from the Android team itself. As it 'probably doesn't matter', I'm going to leave my code as it stands. If it ain't broke I ain't going to fix it.Eanore
G
12

I've never faced any problems calling the super methods before my code and I'm pretty sure I wouldn't have faced any problems if I had called the super methods after my code. But usually super classes instances must be initialized before and deinitialized after subclasses. So ideally you should run your code after calling the super in onCreate(), onStart(), onRestart() and onResume() and before in onPause(), onStop() and onDestroy().

But one more time. All this code is executed on the UI thread so the order of your code doesn't make great sense excluding some rare cases. But the best idea is to read the source code and to understand how it works and what really happens.

Gesner answered 25/8, 2011 at 13:36 Comment(1)
That's what I've been doing (super last in pause, first in resume etc), taking the view that I want to do things like cancelling timers etc, before the super really does the pause/stop/destroy. However Egor's quote is correct, so that's got me worried now.Eanore

© 2022 - 2024 — McMap. All rights reserved.