为什么片段类应该是公共的? [英] Why fragment class should be public?

查看:77
本文介绍了为什么片段类应该是公共的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些线程来解释为什么 Fragment 类应该是 static (而不是 non-static内部)的原因,例如

I found some threads that explain why a Fragment class should be static (not non-static inner) like this. But can't understand the reason for this error message:

此片段类应为公共(...)

This fragment class should be public (...)


我使用的是Android Studio 3.1.2,这是我的代码的一部分:


I use Android Studio 3.1.2 and this is a part of my codes:

public class MainActivity extends android.support.v7.app.AppCompatActivity {
    // ...
}

class DefaultFragment extends android.support.v4.app.Fragment {
    // ...
}

该片段应该仅在 MainActivity 中使用.

The fragment is supposed to be used only in MainActivity.

IDE消息中还有一些其他信息.但是关于构造函数:

There is some other information in IDE message. But are about constructors:

来自Fragment文档:

From the Fragment documentation:

每个片段都必须有一个空的构造函数,因此可以在恢复其活动状态时实例化它.强烈建议子类不要使用带有参数的其他构造函数,因为在重新实例化片段时将不会调用这些构造函数;取而代之的是,参数可以由调用者使用setArguments(Bundle)提供,然后由Fragment使用getArguments()进行检索.

Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().

推荐答案

更改方向时,将重新创建Activity,框架将在片段上创建新实例(并恢复以前的状态).因此,在这里创建Fragment实例时,框架要求提供公共构造函数.

On orientation change, Activity recreated and framework creates new instance on fragment (and restores previous state). So here to create instance of Fragment, framework asks to provide public constructer.

摘录自 Fragment.Fragment()

默认构造函数:

每个片段都必须有一个 empty构造函数,因此可以在恢复其活动状态时将其实例化.强烈建议子类不要使用带有参数的其他构造函数,因为在重新实例化片段时将不会调用这些构造函数;相反,参数可以由调用方使用 setArguments(Bundle)提供,然后由 Fragment 使用 getArguments()检索.

Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().

下面是代码格式 Activity.java 类,您可以通过查找更多

Below is code form Activity.java class and you can analysis further in looking more codes of FragmentManager.dispatchMoveToState()

@MainThread
@CallSuper
protected void onCreate(@Nullable Bundle savedInstanceState) {
    ........
    if (savedInstanceState != null) {
        mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
        mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
                View.LAST_APP_AUTOFILL_ID);
        if (mAutoFillResetNeeded) {
            getAutofillManager().onCreate(savedInstanceState);
        }
        Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
        mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
                ? mLastNonConfigurationInstances.fragments : null);
    }
    mFragments.dispatchCreate();
    .....
}

这篇关于为什么片段类应该是公共的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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