I have activity MainActivity with a button.
When the button is pressed, I start a new activity called ShowPictureActivity (which has one fragment called ShowPictureFragment).
code to launch the ShowPictureActivity
intent = new Intent(LeakActivity.this, ShowPictureActivity.class);
// parameters that I would like to pass to the ShowPictureFragment
intent.putExtra("nRecNum", 1); // sample number
intent.putExtra("pictureFileSpec", "picture/pictures/leak/leak.png"); // sample string
startActivity(intent);
I know how to use the intent.putExtra to pass parameters to the activity ShowPictureActivity and I do know of a method to get the parameters from the fragment using the following code:
ShowPictureFragment class
@Override
public View onCreateView
{
.
.
.
// get parameters passed in the intent object
intent = getActivity().getIntent();
nRecNo = intent.getIntExtra("nRecNo", -1);
pictureFileSpec = intent.getStringExtra("pictureFileSpec");
.
.
From my reading, this is the not the best way to do this. I read that I should be using a bundle.
I saw a snippet of code to build a bundle, but I don't know how to get it to the ShowPictureFragement.
// my sample bundle to pass to the ShowPictureFragment
Bundle bundle = new Bundle();
bundle.putInt("nRecNo", 3);
bundle.putCharSequence("pictureFileSpec", "my\pictures\picture.png");
Can someone explain or give me an example showing how to create the bundle and pass it onto the a fragment.
Thank you in advance!