I want to send data from my activity to my fragment. What I'm doing now is the following.
String itemDescription = workAssignmentItem.getDescription();
Bundle bundle = new Bundle();
bundle.putString("itemDescription", itemDescription);
FirstFragment.newInstance(bundle);
Then in my Fragment I do:
public static FirstFragment newInstance(Bundle bundle) {
FirstFragment fragment = new FirstFragment();
fragment.setArguments(bundle);
return fragment;
}
However, when I try to do 'getArguments().getString("itemDescription");'in my onCreate, as so:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
description = getArguments().getString("itemDescription");
}
it will not work. getArguments returns null. I'm not quite sure why it returns null, since multiple sources on the internet say this is the way to do it.
Can anyone point me in the right direction? thanks in advance
You dont need bundle to pass a single string in Fragment
String itemDescription = workAssignmentItem.getDescription();
FirstFragment.newInstance(itemDescription);
Fragment Change to like this :
public static FirstFragment newInstance(String itemDescription){
FirstFragment fragment=new FirstFragment();
bundle args=new Bundle();
args.putString("itemDescription",itemDescription);
fragment.setArguments(args);
return fragment;
}
and onCreate like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
description = getArguments().getString("itemDescription");
}