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 ?