Using newInstance() to Instantiate a Fragment
来源:互联网 发布:linux创建目录下的文件 编辑:程序博客网 时间:2024/05/18 22:50
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)! :)
- Using newInstance() to Instantiate a Fragment
- Android Unable to instantiate fragment
- fragment Trying to instantiate a class com.example.testhuanxindemo.MyFragment that is not a Fragmen
- Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.dy.fragmen
- fragment异常android.app.Fragment$InstantiationException: Unable to instantiate fragment
- Unable to instantiate fragment 的解
- Android "Unable to instantiate fragment"解决方案
- Failed to instantiate * using constructor public * with arguments **
- android.support.v4.app.Fragment$InstantiationException: Unable to instantiate&nbs
- Unable to instantiate fragment make sure class name....
- newInstance创建Fragment
- Kotlin Fragment NewInstance
- ndroid.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment benguo.simphon
- android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment xxx.fragments
- Unable to instantiate fragment com.viewpager.demo.FirstFragment: make sure class name exists, is pub
- android googlemap v2 Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make s
- Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor
- Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor
- 好用的mate-date查看工具
- UFUNCTION
- c/c++值传递和引用传递
- UVA11039 Building designing (贪心)
- Hi3516A开发--烧写/启动模式
- Using newInstance() to Instantiate a Fragment
- 根据沪深300指数编制和调整策略建立股票池
- C++11 并发指南四(<future> 详解三 std::future & std::shared_future)
- AB测试结果分析
- 论文笔记——Rich feature hierarchies for accurate object detection and semantic segmentation
- Vijos P1071 新年趣事之打牌
- 序列化与JavaBean与xml互转
- 一个偶数总能表示为两个素数之和。
- 安卓的Md5加密