Using newInstance() to Instantiate a Fragment(转)
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(转)的更多相关文章
- 【译】使用newInstance()来实例化fragment
		
我最近读到StackOverflow上面关于Fragment实例化的一个问题,觉得挺有趣的. new MyFragment()和MyFragment.newInstance()之间的差别是什么?应该用 ...
 - Creating a Fragment: constructor vs newInstance()
		
from stack overflow and another chapter I recently grew tired of constantly having to know String ke ...
 - 札记:Fragment基础
		
Fragment概述 在Fragment出现之前,Activity是app中界面的基本组成单位,值得一提的是,作为四大组件之一,它是需要"注册"的.组件的特性使得一个Activit ...
 - TabLayout+ViewPager+Fragment制作页卡
		
本人很懒,直接上代码了. 布局文件: <?xml version="1.0" encoding="utf-8"?><android.suppo ...
 - Android Fragment 使用技巧
		
1. Fragment 使用时要有一个无参构造函数 如果没有无参构造函数,而是像按照普通类来使用,只创建有参构造函数,则会出现 android.support.v4.app.Fragment$Inst ...
 - Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数(转)
		
Fragment在Android3.0开始提供,并且在兼容包中也提供了Fragment特性的支持.Fragment的推出让我们编写和管理用户界面更快捷更方便了. 但当我们实例化自定义Fragmen ...
 - Android Fragment基础及使用
		
同一个app内的界面切换 用Fragment比较合适,因为Activity比较重量级 Fragment 轻量级,切换灵活 --------------------------------------- ...
 - Android开发之Fragment传递參数的几种方法
		
Fragment在Android3.0開始提供,而且在兼容包中也提供了Fragment特性的支持. Fragment的推出让我们编写和管理用户界面更快捷更方便了. 但当我们实例化自己定义Fragmen ...
 - Android中Fragment+ViewPager的配合使用
		
官方推荐 ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment,涵盖最常见的用例.FragmentPa ...
 
随机推荐
- 《Thinking in C++》学习笔记(二)【第三章】
			
第三章 C++中的C 3.4.4 指针简介 ‘&’运算符:只要在标识符前加上‘&’,就会得出标识符的地址. C和C++有一个专门存放地址的变量类型.这个变量类型叫做指针(pointer ...
 - Codeigniter 利用加密Key(密钥)的对象注入漏洞
			
http://drops.wooyun.org/papers/1449 原文链接:http://www.mehmetince.net/codeigniter-object-injection-vuln ...
 - 一个Form中2个按钮,PHP后台如何判断提交的是哪一个按钮
			
方法一: <div class="container theme-showcase" role="main"> <form class=&qu ...
 - Appium入门示例(python)
			
安装Python依赖 pip3.4 install nose pip3.4 install selenium pip3.4 install Appium-Python-Client 运行测试用例and ...
 - C# DES 加密 解密
			
//注意:密钥必须为8位 private const string m_strEncryptKey = "abcd1234"; /// <summary> /// 加密 ...
 - ngrok本地反向代理
			
ngrok本地反向代理 ngrok本地反向代理 使用ngrok可以把内网服务映射到外网 国内ngrok服务配置如下 在ngrok.exe所在的目录下添加ngrok.cfg文件 ngrok.cfg文件内 ...
 - 【hdu3065-病毒侵袭持续中】AC自动机
			
题意:给定一些只含大写字母的病毒串,再给一个文本串,问文本串中每个病毒串各出现了多少次. 题解: 就是用AC自动机,在每个节点末尾有个id记录是哪个单词的末尾,然后如果同时是多个单词的末尾就用一个ne ...
 - iOS开发--项目内存优化
			
在用非ARC模式编写iOS程序的时候,造成程序内存泄露在所难免,后期我们一般会进行内存优化.自己比较常用的内存优化方法有两种 1.Analyze,静态分析内存泄露的方法.很简单,在Xcode菜单栏中点 ...
 - JavaWeb笔记——下载文件
			
核心内容是两个头一个流 > 头:Content-Type:你传递给客户端的文件是什么MIME类型,例如:image/pjpeg * 通过文件名称调用ServletCont ...
 - CentOS 加载/挂载 U盘
			
1.以root用户登陆 先加载USB模块 modprobe usb-storage 用fdisk -l 看看U盘的设备 假如U盘是sda1 2.确定在 目录 /mnt 下建立了 文件夹 ...