简体   繁体   English

Android-getActivity和Activity生命周期

[英]Android - getActivity and Activity life cycle

If I have the following code in a Fragment: 如果片段中包含以下代码:

public void doSomething() {
    if (getActivity() == null)
        return; // no null pointer exceptions here!
    // ...
    // do bunch of stuff here
    // ...

    getActivity().setTitle("done something"); // just whatever
}

As you can see, at the beginning of the method I check if getActivity() is null and return if it is. 如您所见,在方法开始时,我检查getActivity()是否为null,如果是,则返回。 However, if it is not null, the method continues executing. 但是,如果它不为null,则该方法继续执行。 At the end, I call getActivity() again to set title, however I don't check if getActivity() is null. 最后,我再次调用getActivity()设置标题,但是我不检查getActivity()是否为null。 Theoretically, it's possible at this point the Fragment has been detached from the Activity so it's possible getActivity() could be null and therefore the last line will throw a NullPointerException. 从理论上讲,这时有可能将Fragment与Activity分离,因此getActivity()可能为null,因此最后一行将抛出NullPointerException。 What is the solution for this? 有什么解决方案? Check if getActivity() is null every time I want to call it? 每次我要调用getActivity()时是否检查它是否为null?

Edit: Even if we try this, it's possible to get a NPE - I just called getActivity() each time to simplify the example: 编辑:即使我们尝试这样做,也有可能获得NPE-我每次都只调用getActivity()来简化示例:

public void doSomething() {
    Activity activity = getActivity();
    if (activity == null)
        return; // no null pointer exceptions here!
    // ...
    // do bunch of stuff here
    // ...

    activity.setTitle("done something"); // just whatever
}

The problem is not that the Activity hasn't finished loading yet - this can happen minutes after load for instance, as a response to any event. 问题不在于活动尚未完成加载-例如,加载后几分钟就可能发生,作为对任何事件的响应。 The problem is not the FIRST call to getActivity, it's that in the 15 lines or so from the time we check if activity is null to the last time we reference it, it can no longer exist. 问题不是对getActivity的第一次调用,而是从检查活动是否为null到上一次引用该活动的15行左右,它不再存在。

I don't have a logcat right now, but this is what is happening. 我现在没有logcat,但这是正在发生的事情。 We haven't really seen this ourselves, but this is for an app where this function is being called thousands of times a day, so it's possible to happen once in a while to someone. 我们自己还没有真正看到它,但是这是针对每天要调用数千次此功能的应用程序,因此有时可能会不时发生。

Edit 2: What I'm currently doing is trying and catching for NPEs and just not doing something if it fails. 编辑2:我目前正在做的是尝试并捕获NPE,如果失败则不做任何事情。 However it seems like an ugly way to solve the problem - I'm wondering if there's a better solution! 但是,这似乎是解决问题的丑陋方法-我想知道是否有更好的解决方案!

Call getActivity once at the top and save it in a variable. 在顶部调用一次getActivity并将其保存在变量中。 Why call it 20 times when it won't change in between (and if it did, using a mix of the old and new could be really bad anyway). 为什么在它之间不改变的情况下调用它20次(如果确实如此,则无论如何将新旧混合使用都是很糟糕的)。

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

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