Android Fragment getActivity() = null

2019-06-15 13:31发布

I'm using fragments in my application. And the very common problem while using them are the NPE when using getActivity(). I know we can solve it by checking if getActivity() != null every single time or checking if the fragment isAdded().

In one of my classes I'm getting activity's context in more than 60 places. Checking if getActivity()is not null or if the fragment is still added to the activity in all the places is making the code ugly,larger and non-maintainable. Is there any other way to handle this? Is it even possible to destroy the fragment(and stop any work it has been doing while being removed) when it is removed from the activity?

Also Is this way a suggested one?

5条回答
Animai°情兽
2楼-- · 2019-06-15 13:40

My solution is override the onSaveInstanceState method in BaseActivity:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //solution of fragment.getActivity() is null
    outState.remove("android:support:fragments");
}
查看更多
仙女界的扛把子
3楼-- · 2019-06-15 13:51

I didn't found a solution to that, maybe because if you think about the lifecycle of a fragment you should be able to understand when you have check the null value.

查看更多
时光不老,我们不散
4楼-- · 2019-06-15 13:59

In my experience, most cases of getActivity() returning null are in asynchronous callbacks.

For example, your fragment fires an AsyncTask, and then gets removed before the the background job is done, then when the background job finishes and calls getActivity() in onPostExecute(), it will get a null since the fragment is already detached from the activity.

My solution:

1.Check getActivity()==null at the beginning of every asynchronous callback, if it's the case then just abort the method.

2.Cancel asynchronous jobs in onDetach().

And I think this is a better solution than saving the activity instance in onAttach(), because since your fragment is removed, why bother doing all the jobs left in the callbacks(in most cases UI codes)?

查看更多
别忘想泡老子
5楼-- · 2019-06-15 14:00

I think you should use the Fragment's onAttach(Activity) method.

I think that should help you avoid all of those NPE.

查看更多
再贱就再见
6楼-- · 2019-06-15 14:01

getActivity will be reinitilized in method - onActivityCreated().

So it's safer to call getActivity() right after onActivityCreated() (according to lifecycle of fragments http://developer.android.com/guide/components/fragments.html) - for example in onStart() - in that case it's WILL BE NEVER NULL - no need to do useless checks like isAdded and getActivity != null.

P.S. If we use that solution:

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

mActivity will be never null - but later in method onActivityCreated() getActivity() became different with mActivity. My opinion - sure we can save whole activity in variable, but it's safer to follow android fragments lifecycle workflow and get activity right after onActivityCreated()

查看更多
登录 后发表回答