简体   繁体   English

什么时候调用super.onPause()?

[英]When to call super.onPause()?

I am implementing Analytics in my android application, and I would like advice on when to call super.onPause() 我正在我的android应用程序中实现Google Analytics(分析),我想了解何时调用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? super.onPause()执行上传操作之后调用super.onPause()什么作用?

In general, when should one call super.onPause() ? 通常,何时应该调用super.onPause()

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): 所选答案不正确,(我知道这是一个老问题,但是对于新读者来说,这是正确的方法: 在Super.onPause或Super.OnStart之后添加代码,...这是您问题的Android参考(直接链接在评论中):

Quote from Activities: Your implementation of these life-cycle methods must always call the superclass implementation before doing any work. 从活动中引用:这些生命周期方法的实现必须始终在执行任何工作之前调用超类实现。

You only call super.onPause() in your own Activity.onPause() override. 您只能在自己的Activity.onPause()覆盖中调用super.onPause()

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. 如果要覆盖它,则不要在此处进行缓慢的处理,否则可能会得到ANR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM