为什么加入片段,当你检查savedInstanceState == NULL?当你、片段、NULL、savedInstanceState

2023-09-07 14:28:34 作者:柠檬心〆微酸

在片段文档的,在该实例中的一个,它们检查 savedInstanceState == NULL 添加片段时:

 公共静态类DetailsActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        如果(getResources()。getConfiguration()。方向
                == Configuration.ORIENTATION_LANDSCAPE){
            //如果屏幕现在是在横向模式下,我们可以显示
            //对话框中,符合列表,所以我们不需要这个活动。
            完();
            返回;
        }

        如果(savedInstanceState == NULL){
            //在初始设置,插上的细节片段。
            DetailsFragment细节=新DetailsFragment();
            details.setArguments(getIntent()getExtras());
            。getFragmentManager()的BeginTransaction()加(android.R.id.content,详情).commit()。
        }
    }
}
 

这是什么检查的目的是什么?会发生什么,如果它不存在?

解决方案   

此检查的目的是什么?

巡回检察组 十大逻辑漏洞,这才是评分走低的根本原因

要不能添加片段的两倍,虽然我preFER检查,看看是否该片段是依靠了那里,而不是捆绑

  

会发生什么,如果它不存在?

起初,什么都没有,因为捆绑时,活动的第一创建。

然而,然后,用户旋转设备的屏幕从纵向到横向。或者,用户改变语言。或者,用户将设备插入制造商提供的汽车停靠。或者,用户执行任何其他配置变化。

您的活动将被销毁,默认情况下重建。您的片段也将被销毁,默认情况下(例外:那些在其 setRetainInstance(真)被称为,这是从旧的活动卸下并安装到新的)重建

因此​​,在第二的时间的活性被创建 - 创建作为配置变化的结果的实例 - 你的片段的已存在的,因为它是要么重新创建或保留。你不想要的片段(通常)的第二个实例,因此,你采取措施来检测,这已经发生了,而不是运行一个新的 FragmentTransaction

In the fragment doc, in one of the example, they check for savedInstanceState == null when adding a fragment:

public static class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}

What is the purpose of this check? What would happen if it is not there?

解决方案

What is the purpose of this check?

To not add the fragment twice, though I prefer checking to see if the fragment is there instead of relying on that Bundle being null.

What would happen if it is not there?

Initially, nothing, as the Bundle will be null when the activity is first created.

However, then, the user rotates the device's screen from portrait to landscape. Or, the user changes languages. Or, the user puts the device into a manufacturer-supplied car dock. Or, the user does any other configuration change.

Your activity will be destroyed and recreated by default. Your fragments will also be destroyed and recreated by default (exception: those on which setRetainInstance(true) are called, which are detached from the old activity and attached to the new one).

So, the second time the activity is created -- the instance created as a result of the configuration change -- your fragment already exists, as it was either recreated or retained. You don't want a second instance of that fragment (usually), and therefore you take steps to detect that this has occurred and not run a fresh FragmentTransaction.