fragment pass data to fragment code example
Example 1: i want to select data from one fragment and want to update in another
Bundle bundle = new Bundle();
bundle.putString("key","abc");
Fragment_2 fragment2 = new Fragment_2();
fragment2.setArguments(bundle);
getFragmentManager()
.beginTransaction()
.replace(R.id.content, fragment2)
.commit();
Bundle bundle = this.getArguments();
if(bundle != null){
}
Example 2: pass data from activity to fragment android
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
And inside fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}