Best Practice For Instantiating a New Android Fragment

Best practice for instantiating a new Android Fragment

If Android decides to recreate your Fragment later, it's going to call the no-argument constructor of your fragment. So overloading the constructor is not a solution.

With that being said, the way to pass stuff to your Fragment so that they are available after a Fragment is recreated by Android is to pass a bundle to the setArguments method.

So, for example, if we wanted to pass an integer to the fragment we would use something like:

public static MyFragment newInstance(int someInt) {
MyFragment myFragment = new MyFragment();

Bundle args = new Bundle();
args.putInt("someInt", someInt);
myFragment.setArguments(args);

return myFragment;
}

And later in the Fragment onCreate() you can access that integer by using:

getArguments().getInt("someInt", 0);

This Bundle will be available even if the Fragment is somehow recreated by Android.

Also note: setArguments can only be called before the Fragment is attached to the Activity.

This approach is also documented in the android developer reference: https://developer.android.com/reference/android/app/Fragment.html

Understanding the Fragment.newInstance method

You can name the function however you like: newInstance, getInstance, newFragment. It doesn't matter, it is only a helper method. Important is that you put all your arguments with fragment.setArguments(args). Android system will remember those arguments and will use them when fragment will be recreated.

public static MyFragment newInstance(int arg) {

Bundle args = new Bundle();
args.putInt("ARG", arg);

MyFragment fragment = new MyFragment();
fragment.setArguments(args);
return fragment;
}

fragments best practices

onCreateView()

This is the place where you initialize your views (findViewById()) / attach listeners to them.

From android docs:

Called to have the fragment instantiate its user interface view

onCreate()

It's better to move the code that connects to the database here - code that doesn't depend on UI elements.

You can read more in the official docs here.

Best way to instantiate a fragment?

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

When Fragment.instantiate is preferable over MyFragment.newInstance or new MyFragment()

instantiate() allows you to specify a fragment by name, without compile-time static resolution of the class.

It's useful when the fragment name comes from some runtime source, such as a binary XML:

<fragment class="com.example.FragmentClass" ...

This is how the framework instantiates fragments specified in layout XML.

In code, it's preferable to use newInstance() or the empty constructor to get compile-time static type checking.

The code instantiate() does under the hood is not too different from what happens when instantiated with newInstance() / empty constructor so there's unlikely to be a significant difference in performance.

android navigation pass arguments to fragment constructor

Short answer is no, you can not pass arguments to Fragment.

All subclasses of Fragment must include a public no-argument constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the no-argument constructor is not available, a runtime exception will occur in some cases during state restore.



Related Topics



Leave a reply



Submit