-3

Method 1

public static MyFragment newInstance(int index) {

    MyFragment f = new MyFragment();

    Bundle args = new Bundle(1);

    args.putInt("index", index);

    f.setArguments(args);

    return f;

  }

Usge

Myfragment.newInstance(1);

Method 2

 public MyFragment newInstance(int index) {

    Bundle args = new Bundle(1);

    args.putInt("index", index);

    setArguments(args);

    return this;
  }

Usge

new Myfragment().newInstance(1);

In the above snippets which one is more appropriate and preferable way and please point out why ?

And now am doing this..

 List<Fragment> fragments = new ArrayList<>();
            fragments.add(new MyFragment().newInstance(defaultId));
            int i = 1;
            for (Categories categories : mCategories) {
                String id = categories.getCategory_id();
                String name = categories.getCategory_name();
//                String slno = categories.getSlno();
                fragments.add(new MyFragment().newInstance(defaultId));
                Titles[i] = name;
                i++;
            }

Anything wrong with this ?

Christ
  • 155
  • 2
  • 16

2 Answers2

2

Method 1 would be preferred over Method 2.

That's because in Method 1 you really create a MyFragment object. In Method 2 you first create a MyFragment object and then you initialize it with newInstance(...). If you want to use method 2 i'd suggest to do it in 2 lines:

MyFragment frag = new MyFragment();
frag.initialize(1);

with the initialize method:

public void initialize(int index) {
    Bundle args = new Bundle(1);
    args.putInt("index", index);
    setArguments(args);
}
Parker_Halo
  • 505
  • 2
  • 19
  • Plz tell me..Wat's the wrong with the code in method 2 i had written in my qustion.?? – Christ Oct 27 '15 at 05:58
  • well it's easy: you already create a new instance with `new MyFragment()`. After that you use a method called `newInstance()`. which implies that it creates (another) new instance which it doesn't! It's not wrong what you do there but it's neither nice nor the common way to do it! – Parker_Halo Oct 27 '15 at 07:39
1

Go ahead with Method 1. Always try to use Static Factory Methods over Constructors. Why you need to use this could be found out in the famous book Effective Java By Joshua Bloch: Item1 - Static Factory Method.

Also you could refer: Effective Java By Joshua Bloch: Item1 - Static Factory Method

Community
  • 1
  • 1
Henry
  • 17,490
  • 7
  • 63
  • 98
  • I'm looping the fragments and adding to `List fragments = new ArrayList<>();` if it static the memory wont get freed when destroy the activity know ? – Christ Oct 26 '15 at 11:44
  • 1
    This is a static method to create the Fragment object. That's all. So there is no way this will cause any memory problem. The Fragment object will not have reference inside this method. Kindly read 'Item 1' on Effective Java. – Henry Oct 26 '15 at 11:48
  • If i use static method might be use static variables too..wat about that ? `private static final String ARG_PAGE_ONE = "arg1"; private static final String ARG_PAGE_TWO = "arg2"; private static final String ARG_PAGE_THREE = "arg3"; public static HomeScreenTabs create(String categoryId, String categoryName, String userId, boolean isGuest) { HomeScreenTabs ob=new HomeScreenTabs(); Bundle bdl = new Bundle(4); bdl.putString(ARG_PAGE_ONE, categoryId); bdl.setArguments(bdl); return ob; }` – Christ Oct 26 '15 at 12:03
  • So at every refresh the memory of those variables won't disallocate right ? – Christ Oct 26 '15 at 12:05
  • There will be a huge number of static variable stay alive even after on activity distroy at every refresh of page.. – Christ Oct 26 '15 at 12:06
  • No. Only static methods, not static variable. `newInstance()` is not just a static method. It's a `static factory method`. – Henry Oct 26 '15 at 13:01