当我们片段添加到特定的布局,
我们可以使用folloing码
Fragment fragment = new SampleFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_layout, fragment);
fragmentTransaction.commit();
或
Fragment fragment = SampleFragment.getInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_layout, fragment);
fragmentTransaction.commit();
我不明白是什么区别片段对象定义句子。 从某些来源,当使用'Fragment.getInstance()'类似单例模式时,使用'getInstance(Bundle data)'方法参数将包数据传递给片段。
你能告诉我有什么区别吗?
+0
[此链接可能会帮助你,请访问这里一次](http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment) –
+0
http:// www .androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html此链接必须帮助您 –