Android Activity 的 onDestroy / Fragment 的 onDestroyView 设置 Null 实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26369905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:19:00  来源:igfitidea点击:

Activity's onDestroy / Fragment's onDestroyView set Null practices

androidandroid-activityandroid-fragmentsondestroy

提问by Kevin Coppock

I am reading ListFragmentsource code and I see this implementation:

我正在阅读ListFragment源代码,我看到了这个实现:

ListAdapter mAdapter;
ListView mList;
View mEmptyView;
TextView mStandardEmptyView;
View mProgressContainer;
View mListContainer;
CharSequence mEmptyText;
boolean mListShown;

/**
 * Detach from list view.
 */
@Override
public void onDestroyView() {
    mHandler.removeCallbacks(mRequestFocus);
    mList = null;
    mListShown = false;
    mEmptyView = mProgressContainer = mListContainer = null;
    mStandardEmptyView = null;
    super.onDestroyView();
}

In this function, Google developers set Null to all view fields that declared in ListFragment and remove callback 'mRequestFocus'.

在此函数中,Google 开发人员将所有在 ListFragment 中声明的视图字段设置为 Null,并删除回调“mRequestFocus”。

In ListActivitysource code. Google developers implemented like below:

ListActivity源代码中。谷歌开发者实现如下:

protected ListAdapter mAdapter;
protected ListView mList;

private Handler mHandler = new Handler();


@Override
protected void onDestroy() {
    mHandler.removeCallbacks(mRequestFocus);
    super.onDestroy();
}

I didn't see Google developers set Null to mList on onDestroy of ListActivity as they did for ListFragment class.

我没有看到 Google 开发人员在 ListActivity 的 onDestroy 上将 Null 设置为 mList,就像他们对 ListFragment 类所做的那样。

My question is

我的问题是

  1. Why google developers didnot set Null to mList in onDestroy of ListActivity? Any reasons?

  2. Do we need to set Null to all View fields in Activity's onDestroy and Fragment's onDestroyView?

  1. 为什么google开发者没有在ListActivity的onDestroy中将Null设置为mList?有什么原因吗?

  2. 我们需要将Activity的onDestroy和Fragment的onDestroyView中的所有View字段都设置为Null吗?

3. Any practices for set Null in these two functions: Activity's onDestroy and Fragment's onDestroyView?

3. Activity的onDestroy和Fragment的onDestroyView这两个函数中set Null的做法有哪些?

Thank you for your ideas!

谢谢你的想法!

回答by Kevin Coppock

So the reason it's different between Fragments and Activities is because their lifecycles are different. When an Activityis destroyed, it's going away for good. However, Fragmentsmay create and destroy their views multiple times before they're actually destroyed. For clarification, in an Activity:

所以 Fragment 和 Activities 不同的原因是因为它们的生命周期不同。当一个Activity被摧毁时,它就会永远消失。但是,Fragments可能会在它们实际销毁之前多次创建和销毁它们的视图。为澄清起见,在活动中:

onDestroy()
onCreate()

will never happen in sequence for the same Activity instance. For a Fragment, the following is perfectly valid:

对于同一个 Activity 实例,永远不会按顺序发生。对于 Fragment,以下内容完全有效:

onCreate()
onCreateView()
onDestroyView()
onCreateView()
onDestroyView()
onDestroy()

One case where you can see this is when a Fragmentgoes into the back stack. Its view will be destroyed (as it is no longer visible) but the instance will remain around to be easily resumed when the user presses back to return to it (at which point onCreateView()will again be called).

您可以看到的一种情况是当 aFragment进入后堆栈时。它的视图将被销毁(因为它不再可见),但是当用户按回返回它时,实例将保留在周围以便轻松恢复(此时onCreateView()将再次被调用)。

After onDestroyView(), you can (and likely should) release all of your Viewreferences to allow them to be garbage collected. In many cases, it's not necessary, as if it's just happening during a configuration change, onDestroy()will immediately follow and the whole instance will be garbage collected.

之后onDestroyView(),您可以(并且可能应该)释放所有View引用以允许它们被垃圾收集。在许多情况下,这是没有必要的,就好像它只是在配置更改期间发生一样,onDestroy()会立即跟进并且整个实例将被垃圾收集。

Essentially, I would say it is good practice to release any and all view references in onDestroyView(), and could save quite a bit of memory if your app has a large backstack.

从本质上讲,我会说释放 中的任何和所有视图引用是一种很好的做法,onDestroyView()如果您的应用程序有大量的后台堆栈,可以节省相当多的内存。

回答by Volodymyr Lykhonis

No need to set null if that does not influence on logic of the app. E.g. if (mList == null) ...

如果不影响应用程序的逻辑,则无需设置 null。例如 if (mList == null) ...