用null参数调用super.onCreate()吗? [英] Calling super.onCreate() with null parameter?

查看:89
本文介绍了用null参数调用super.onCreate()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onCreate(Bundle bdl){}中,我们必须通过super.onCreate(bdl)调用其超级构造函数.

In onCreate(Bundle bdl){}, we must call its super constructor by super.onCreate(bdl).

对于新创建的活动,我们在onCreate(Bundle bdl){}中得到了一个空捆绑.因此,当我们调用super.onCreate(bdl)时,它与调用super.onCreate(null)相同.

For newly created activities, we got a null Bundle in onCreate(Bundle bdl){}. So when we call super.onCreate(bdl), it is the same as calling super.onCreate(null).

对于重建的活动(如旋转后),我们得到了一个非空的捆绑包.但是我注意到,即使我们调用super.onCreate(null)而不是super.onCreate(bdl),也似乎是相同的.布局恢复工作在super.onRestoreInstanceState(bdl)中完成.

For reconstructed activities (like after rotate), we got a non-null Bundle. But I notice even if we call super.onCreate(null), instead of super.onCreate(bdl), it seems to be just the same. The layout restoration works are done in super.onRestoreInstanceState(bdl).

那么,在所有情况下调用super.onCreate(null)与调用super.onCreate(bdl)真的一样吗?

So, is it really true that calling super.onCreate(null) is the same as calling super.onCreate(bdl) in all the cases?

谢谢.

推荐答案

根据Android源代码,Activity.onCreate()方法将saveInstanceState捆绑包转发到活动的片段.更具体地说,它使用"android:fragments"键获取一个可分割区域,并使用FragmentManager.restoreAllStates()方法将此可分割区域转发给片段,该方法本身会恢复所有片段的状态.

According to the Android Source code, the Activity.onCreate() method forwards the saveInstanceState bundle to the activity's fragments. To be more specific, it fetches a parcelable with the "android:fragments" key and forwards this parcelable to the fragments using the FragmentManager.restoreAllStates() method, which itself restore the state on all fragments.

Activity.onRestoreInstanceState()方法将分发包转发到活动的窗口.再次从保存的实例中获取"android:viewHierarchyState"捆绑包,然后使用Window.restoreHierarchyState()方法将其转发到窗口.

The Activity.onRestoreInstanceState() method forwards the bundle to the activity's window. Again it fetches the "android:viewHierarchyState" bundle from the saved instance and forwards it the the window using the Window.restoreHierarchyState() method.

所以要回答您的问题,如果您的活动未使用片段",则实际上调用super.onCreate(null)不会改变任何内容.但是,作为最佳实践,我建议您始终转发确切的savedInstance捆绑包(除非您知道自己在做什么).

So to answer your question, if your activity doesn't use Fragments, then indeed calling super.onCreate(null) won't change anything. But as best practice, I'll advise you to always forward the exact savedInstance bundle (unless you know what you're doing).

这是我所讨论的示例源代码,摘自AOSP v17:

Edit : here are the sample source codes I talked about, taken from AOSP v17 :

Activity.java

protected void onCreate(Bundle savedInstanceState) {

    // [... some content ellipsed for readability purposes]

    if (savedInstanceState != null) {
        Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
        mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
                ? mLastNonConfigurationInstances.fragments : null);
    }
    mFragments.dispatchCreate();
    getApplication().dispatchActivityCreated(this, savedInstanceState);
    mCalled = true;
}


// [...]

protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if (mWindow != null) {
        Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
        if (windowState != null) {
            mWindow.restoreHierarchyState(windowState);
        }
    }
}

这篇关于用null参数调用super.onCreate()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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