12

I am implementing Analytics in my android application, and I would like advice on when to call super.onPause()

if (mAnalyticsSession != null) {
    mAnalyticsSession.close();
    mAnalyticsSession.upload();
}

super.onPause();

What is the effect of calling super.onPause() after doing upload actions vs. before?

In general, when should one call super.onPause()?

Jonik
  • 80,077
  • 70
  • 264
  • 372

2 Answers2

20

The selected answer is not correct, (I know this is an old question but for new readers here is the correct way: Add your codes after Super.onPause or Super.OnStart ,... And here is an Android reference for your question(direct link is in comment):

Quote from Activities: Your implementation of these life-cycle methods must always call the superclass implementation before doing any work.

bastami82
  • 5,955
  • 7
  • 33
  • 44
4

You only call super.onPause() in your own Activity.onPause() override.

public class YourActivity extends Activity {
    @Override
    public void onPause() {
        super.onPause();
        // Do your stuff, e.g. save your application state
    }
}

Note that you don't need to override this if you don't need it. If you're going to override it, then do not make slow processes in here or you might get an ANR.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • So you recommend calling super.onPause() first and then other actions? – Android Developer Jan 15 '13 at 22:46
  • 1
    ok. I was just a bit concerned about putting them before super.onPause() but the analytics sdk guidelines said it needed to be before. – Android Developer Jan 15 '13 at 22:55
  • 4
    As indicated by @Bastami1982 below, super.onPause() should be called before any other code. The reference he mentions can be found here: http://developer.android.com/guide/components/activities.html – bkurzius Nov 20 '15 at 16:51
  • @bkurzius You're right. That's what the code in my answer shows and that's how I always do it. – m0skit0 Nov 20 '15 at 20:46
  • 1
    @m0skit0 - i voted it down because your comment above says it "Doesn't really matter". But if you update your answer I'll be happy to vote it back up :-) – bkurzius Nov 20 '15 at 23:28
  • @bkurzius I already said in the comments that you're right and I'm wrong. My answer doesn't need any update since super.onPause() is called first. – m0skit0 Nov 20 '15 at 23:49