This question already has an answer here:
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();
Or you could use the newInstance method - create a method inside your Fragment class like:
So to create your fragment you do:
(This is used instead of your
new TransactionDetailsFragment()
)Then for example in onCreate/onCreateView of the same fragment you get the value like this:
How about creating a parameterized constructor for
TransactionDetailsFragment
?When you create
new TransactionDetailsFragment(YOUR_PARAMS)
as a param ofFragmentTransaction
, I think using constructor is a good choice.You can use Bundle:
Then, you retrieve as follows: