zoukankan      html  css  js  c++  java
  • 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(有译文)

  • 相关阅读:
    MATLAB 模板匹配
    ACDSee15 教你如何轻松在图片上画圈圈、画箭头、写注释
    Qt 显示一个窗体,show()函数和exec()函数有什么区别?
    Qt 将窗体变为顶层窗体(activateWindow(); 和 raise() )
    Qt QSS样式化 菜单Qmenu&QAction
    Qt 获取文件夹中的文件夹名字
    Qt 删除文件夹或者文件
    欧洲终于承认“工业4.0”失败,互联网经济严重落后中美
    深入浅出数据结构
    浅谈城市大脑与智慧城市发展趋势
  • 原文地址:https://www.cnblogs.com/hubing/p/4722558.html
Copyright © 2011-2022 走看看