I have the following simple code to switch from one fragment to another in the content frame. Is there a simple way to pass variables in the following code?
FragmentManager fm = getActivity().getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new TransactionDetailsFragment()).commit();
You can use Bundle:
FragmentManager fm = getActivity().getFragmentManager();
Bundle arguments = new Bundle();
arguments.putInt("VALUE1", 0);
arguments.putInt("VALUE2", 100);
MyFragment myFragment = new Fragment();
fragment.setArguments(arguments);
fm.beginTransaction().replace(R.id.content_frame, myFragment).commit();
Then, you retrieve as follows:
public class MyFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
if (bundle != null) {
int value1 = bundle.getInt("VALUE1", -1);
int value2 = bundle.getInt("VALUE2", -1);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With