I recently came across an interesting question on StackOverflow regarding Fragment instantiation:

What is the difference between new MyFragment() and MyFragment.newInstance()? Should I prefer one over the other?

Good question. The answer, as the title of this blog suggests, is a matter of proper design. In this case, the newInstance()method is a "static factory method," allowing us to initialize and setup a new Fragment without having to call its constructor and additional setter methods. Providing static factory methods for your fragments is good practice because it encapsulates and abstracts the steps required to setup the object from the client. For example, consider the following code:

public class MyFragment extends Fragment {

    /**
* Static factory method that takes an int parameter,
* initializes the fragment's arguments, and returns the
* new fragment to the client.
*/
public static MyFragment newInstance(int index) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
} }

Rather than having the client call the default constructor and manually set the fragment's arguments themselves, we provide a static factory method that does this for them. This is preferred over the default constructor for two reasons. One, it's convenient for the client, and two, it enforces well-defined behavior. By providing a static factory method, we protect ourselves from bugs down the line—we no longer need to worry about accidentally forgetting to initialize the fragment's arguments or incorrectly doing so.

Overall, while the difference between the two is mostly just a matter of design, this difference is really important because it provides another level of abstraction and makes code a lot easier to understand.

Feel free to leave a comment if this blog post helped (it will motivate me to write more in the future)! :)

http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html

http://www.cnblogs.com/kissazi2/p/4127336.html(有译文)

Using newInstance() to Instantiate a Fragment(转)的更多相关文章

  1. 【译】使用newInstance()来实例化fragment

    我最近读到StackOverflow上面关于Fragment实例化的一个问题,觉得挺有趣的. new MyFragment()和MyFragment.newInstance()之间的差别是什么?应该用 ...

  2. Creating a Fragment: constructor vs newInstance()

    from stack overflow and another chapter I recently grew tired of constantly having to know String ke ...

  3. 札记:Fragment基础

    Fragment概述 在Fragment出现之前,Activity是app中界面的基本组成单位,值得一提的是,作为四大组件之一,它是需要"注册"的.组件的特性使得一个Activit ...

  4. TabLayout+ViewPager+Fragment制作页卡

    本人很懒,直接上代码了. 布局文件: <?xml version="1.0" encoding="utf-8"?><android.suppo ...

  5. Android Fragment 使用技巧

    1. Fragment 使用时要有一个无参构造函数 如果没有无参构造函数,而是像按照普通类来使用,只创建有参构造函数,则会出现 android.support.v4.app.Fragment$Inst ...

  6. Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数(转)

    Fragment在Android3.0开始提供,并且在兼容包中也提供了Fragment特性的支持.Fragment的推出让我们编写和管理用户界面更快捷更方便了.   但当我们实例化自定义Fragmen ...

  7. Android Fragment基础及使用

    同一个app内的界面切换 用Fragment比较合适,因为Activity比较重量级 Fragment 轻量级,切换灵活 --------------------------------------- ...

  8. Android开发之Fragment传递參数的几种方法

    Fragment在Android3.0開始提供,而且在兼容包中也提供了Fragment特性的支持. Fragment的推出让我们编写和管理用户界面更快捷更方便了. 但当我们实例化自己定义Fragmen ...

  9. Android中Fragment+ViewPager的配合使用

    官方推荐 ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment,涵盖最常见的用例.FragmentPa ...

随机推荐

  1. 学习android的博客

    http://www.cnblogs.com/hll2008/http://svn.apache.org/repos/asf/http://blog.csdn.net/chenzheng_javaht ...

  2. App接口设计思路

    http://www.techweb.com.cn/network/system/2016-01-11/2256859.shtml http://www.woshipm.com/pmd/172952. ...

  3. 开源DBCP、C3P0、Proxool 、 BoneCP连接池的比较

    简介 项目主页 使用评价  DBCP DBCP是一个依赖Jakarta commons-pool对象池机制的数据库连接池.DBCP可以直接的在应用程序用使用 http://homepages.nild ...

  4. hdu 4023 Game 博弈论

    思路: 将15种分成5类: 1.1和2为一类: 2.3,4,5,6为一类: 3.7,8,9,10为一类: 4.11,12,13,14,15为一类: 5.15为一类. 比较各类的优先级,就会发现放置的顺 ...

  5. JDBC第三次学习

    这是我的JDBC第三次学习了,在学习的过程中,老是会忘掉一些知识,不记下笔记实在不行啊! 使用JDBC调用存储过程 (1)关于如何使用Navicat(11.1.13) for MySQL如何创建存储过 ...

  6. spring_150804_controller

    实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...

  7. 配置IIS应用程序池

    IIS 6的核心在于工作进程隔离模式,而应用程序池则是定义工作进程如何进行工作,因此,可以说应用程序池是整个IIS 6的核心. 和IIS 5中只能使用单个应用程序池不同,工作在工作进程隔离模式的IIS ...

  8. Unable to open debugger port : java.net.ConnectException “Connection refused”

    http://stackoverflow.com/questions/28283087/unable-to-open-debugger-port-java-net-connectexception-c ...

  9. QAQ OI生涯の最后一个月

    QAQ 总觉得自己要做点什么 可是并不知道去做些什么 QAQ 先挖一些坑吧,不管怎么样,把这些坑填完估计NOI也就无憾了 1.读完13-16的论文 QAQ 2.做完12-16的POI  QAQ 3.做 ...

  10. OpenStack学习系列-----第一篇 OpenStack介绍

    刚开始接触OpenStack,被它所承诺的前景,以及现在业界对它的期望吸引(OpenStack被誉为21世纪的Linux开源社区,可以预见其的发展前景是何其广阔.).怎么说呢,我现在也暂时相信,Ope ...