Activity 的 onDestroy/Fragment 的 onDestroyView 设置 Null 实践 [英] Activity's onDestroy / Fragment's onDestroyView set Null practices

查看:203
本文介绍了Activity 的 onDestroy/Fragment 的 onDestroyView 设置 Null 实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am reading ListFragment source code and I see this implementation:

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();
}

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

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

ListActivity 源代码中.Google 开发者实现如下:

In ListActivity source code. Google developers implemented like below:

protected ListAdapter mAdapter;
protected ListView mList;

private Handler mHandler = new Handler();


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

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

我的问题是

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

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

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

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

3.Activity 的 onDestroy 和 Fragment 的 onDestroyView 这两个函数有没有设置 Null 的做法?

谢谢你的想法!

推荐答案

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

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

onDestroy()
onCreate()

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

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

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

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

One case where you can see this is when a Fragment goes 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).

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

After onDestroyView(), you can (and likely should) release all of your View references 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() 中释放任何和所有视图引用是一种很好的做法,如果您的应用程序有大量后台堆栈,可以节省相当多的内存.

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.

这篇关于Activity 的 onDestroy/Fragment 的 onDestroyView 设置 Null 实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆