Android中的"new Fragment()"和"Fragment.getInstance()"有什么区别? [英] What is difference between 'new Fragment()' and 'Fragment.getInstance()' in Android?

查看:168
本文介绍了Android中的"new Fragment()"和"Fragment.getInstance()"有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android编程中,

In android programming,

当我们将片段添加到特定布局时,

When we add fragment to specific layout,

我们可以使用以下代码

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)'方法参数将捆绑包数据传递给片段.

I cannot understand what is difference between that fragment object define sentence. From some sources, when use 'Fragment.getInstance()' like singleton pattern, pass bundle data to fragment with 'getInstance(Bundle data)' method parameter.

你能告诉我有什么区别吗?

Could you tell me what difference?

推荐答案

首先要注意的是,如果系统破坏了您的片段并必须重新创建它,它将使用no-args调用构造函数.这意味着您必须将参数保存在以后使用的位置(您不能使用args创建构造函数).

First thing to note is that if the system destroyed your fragment and has to re-create it, it will call the constructor with no-args. This thing implies that you have to save your arguments somewhere to be used later (You can't create a constructor with args).

现在,让我们回到您的问题.目前,这两个代码块几乎相同,但仅适用于您提供的示例.如果您应将一些参数传递给片段,则情况会有所不同. getInstance 应该将所需的参数添加到片段中,以确保将来可以使用它们.

Now, let's get back to your question. For the moment, the 2 blocks of code are almost identical, but only for the example you provided. If you should pass some params to your fragment, things are a little different. The getInstance should add the needed arguments to your fragment, guaranteeing that they will be available in a future moment.

我个人使用 getInstance/newInstance (您可能会发现它的变化形式,现在,使用 newInstance 在Android Studio中创建模板片段)我在该片段中需要的参数.例如,如果我在该片段中需要两个字符串,则将它们传递给 getInstance 方法并将其保存在fragment参数中,以确保如果重新创建该片段,它们将可用.

Personally, I use the getInstance/newInstance (you may find variation of it, right now, creating a template fragment in Android Studio use the newInstance) method passing the parameters that I need in that fragment. For example, if I need two strings in that fragment, I will pass them to getInstance method and save them in the fragment arguments to make sure that they will be available if the fragment is re-created.

public static MyFragment getInstance(String param1, String param2) {
    MyFragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putString(KEY_ONE, param1);
    args.putString(KET_TWO, param2);
    fragment.setArguments(args);
    return fragment;
}

当然,对于此方法,您可以传递 Bundle ,但是我认为以这种方式更加清晰,指定了片段将使用的每个参数.

Of course, for this method you can pass a Bundle, but I think that is a little bit clearer in this way, specifying each parameter the fragment will use.

话虽如此,如果您想创建与上述代码块等效的代码,则应使用以下代码:

That being said, if you want to create an equivalent of the above block you should use something like this:

MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
// set your arguments here
fragment.setArguments(args);
// and from here use your code as you did

最后,使用 getInstance 可以停止代码重复(请注意,如果您要创建片段10次,则应复制上述代码10次),并且,如果您创建片段正确(第二段代码),您实际上不必使用 getInstance 方法,但是建议这样做.

To conclude, using getInstance is used to stop the code repetition (note that if you should create the fragment 10 times, you should copy the above code 10 times) and, if you create the fragment correctly (second block of code) you don't really have to use the getInstance method, but it's recommended.

这篇关于Android中的"new Fragment()"和"Fragment.getInstance()"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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