How to transfer some data to another Fragment
likewise it was done with extras
for intents
?
Just to extend previous answers - it could help someone. If your getArguments()
returns null
, put it to onCreate()
method and not to constructor of your fragment:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int index = getArguments().getInt("index");
}
If you are using graph for navigation between fragments you can do this: From fragment A:
Bundle bundle = new Bundle();
bundle.putSerializable(KEY, yourObject);
Navigation.findNavController(view).navigate(R.id.fragment, bundle);
To fragment B:
Bundle bundle = getArguments();
object = (Object) bundle.getSerializable(KEY);
Of course your object must implement Serializable
Complete code of passing data using fragment to fragment
Fragment fragment = new Fragment(); // replace your custom fragment class
Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
bundle.putString("key","value"); // use as per your need
fragment.setArguments(bundle);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(viewID,fragment);
fragmentTransaction.commit();
In custom fragment class
Bundle mBundle = new Bundle();
mBundle = getArguments();
mBundle.getString(key); // key must be same which was given in first fragment
This is how you use bundle:
Bundle b = new Bundle();
b.putInt("id", id);
Fragment frag= new Fragment();
frag.setArguments(b);
retrieve value from bundle:
bundle = getArguments();
if (bundle != null) {
id = bundle.getInt("id");
}
To extend the previous answer even more, like Ankit was saying, for complex objects you need to implement Serializable. For example, for the simple object:
public class MyClass implements Serializable {
private static final long serialVersionUID = -2163051469151804394L;
private int id;
private String created;
}
In you FromFragment:
Bundle args = new Bundle();
args.putSerializable(TAG_MY_CLASS, myClass);
Fragment toFragment = new ToFragment();
toFragment.setArguments(args);
getFragmentManager()
.beginTransaction()
.replace(R.id.body, toFragment, TAG_TO_FRAGMENT)
.addToBackStack(TAG_TO_FRAGMENT).commit();
in your ToFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
MyClass myClass = (MyClass) args
.getSerializable(TAG_MY_CLASS);
From Activity Class:
Send the data using bundle arguments to the fragment and load the fragment
Fragment fragment = new myFragment();
Bundle bundle = new Bundle();
bundle.putString("pName", personName);
bundle.putString("pEmail", personEmail);
bundle.putString("pId", personId);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
fragment).commit();
From myFragment Class:
Get the arguments from the bundle and set them to xml
Bundle arguments = getArguments();
String personName = arguments.getString("pName");
String personEmail = arguments.getString("pEmail");
String personId = arguments.getString("pId");
nameTV = v.findViewById(R.id.name);
emailTV = v.findViewById(R.id.email);
idTV = v.findViewById(R.id.id);
nameTV.setText("Name: "+ personName);
emailTV.setText("Email: "+ personEmail);
idTV.setText("ID: "+ personId);