Best Practice to Instantiate Fragments with Arguments in Android

There are some ways to instantiate and pass data to fragments in android development. However, you must be careful when you do that and you should avoid the wrong approaches while you are instantiating and passing data to fragments.

The most recommended way of instantiate fragments with arguments is to have factory methods for this task.

Also, another important topic is about how to pass data to the fragments while instantiating them. In this case it is tempting to directly access the fields of your fragment and assign their values. However, this is the wrong approach. Because when your application send back to background and the other applications require more and more memory then your application and its resources will be cleared from the memory to open up space to the new ones.


package com.gunhansancar.android.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Sample fragment to demonstrate the instantiation of fragments with arguments
*
* Created by Günhan on 28.10.2015.
*/
public class MyFragment extends Fragment {
private String name;
private int age;
private TextView mNameTextView;
private TextView mAgeTextView;
public static MyFragment newInstance(String name, int age) {
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putInt("age", age);
MyFragment fragment = new MyFragment();
fragment.setArguments(bundle);
return fragment;
}
private void readBundle(Bundle bundle) {
if (bundle != null) {
name = bundle.getString("name");
age = bundle.getInt("age");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sample, container, false);
mNameTextView = (TextView) view.findViewById(R.id.nameTextView);
mAgeTextView = (TextView) view.findViewById(R.id.ageTextView);
readBundle(getArguments());
mNameTextView.setText(String.format("Name: %s", name));
mAgeTextView.setText(String.format("Age: %d", age));
return view;
}
}

view raw

MyFragment.java

hosted with ❤ by GitHub

In this example, to instantiate MyFragment you need to call;

Fragment fragment = MyFragment.newInstance("Gunhan", 28);

If android decides to recreate your activity and fragment, it is going to call no-argument constructor and the system guarantees to pass the arguments bundle to it.

After that when you want to return to your application the android system will create your activity and also your fragment with the default constructor with the passed arguments bundle.

Therefore if you directly set your fields then you are gonna lose their values after recreate operation.

Update: How to use the newly instantiated Fragment in your activity?


FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = MyFragment.newInstance("Gunhan", 28);
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

view raw

activity.java

hosted with ❤ by GitHub

fragment_container could be any layout in your activities main layout xml. It will attach Fragment’s layout on this container layout.
UPDATE 2: DEMONSTRATING HOW TO INSTANTIATE FRAGMENT IN FRAGMENT PAGER ADAPTER

You can find the example project on https://github.com/gunhansancar/FragmentPagerExample