0

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!

3 Answers3

0

You'll want to use the bundle from the intent:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

Source: Passing a Bundle on startActivity()?

Community
  • 1
  • 1
Evan Bashir
  • 5,501
  • 2
  • 25
  • 29
0

send arguments like this

FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
            fragmentTransaction.addToBackStack(null); // push fragment to back stack
            Bundle b = new Bundle();
            b.putSerializable("Obj", mObj);
            InnerFragment innerFrag= new InnerFragment();
            innerFrag.setArguments(b);
            fragmentTransaction.replace(R.id.fragment_content, innerFrag);
            fragmentTransaction.commit();

inside inner fragment fetch the object like this

obj = (Obj) getArguments().get("Obj");
Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47
0

With your question and my answer, I want to be clear for other developers to reference this. One of the best way, I think, is to create a method newInstance into the Fragment that wants the data, which is recommended by Google and other developers. This is to ensure the correct fragment will receive the Bundle data. There is a good Google webpage @ Fragments. Search for "newInstance".

Sample code for passing the Bundle data into ShowPictureFragment, using your sample:

public class ShowPictureFragment extends Fragment {

    public static ShowPictureFragement newInstance(int nRecNo, CharSequence pictureFileSpec) {
        ShowPictureFragment f = new ShowPictureFragment();
        Bundle args = new Bundle();
        args.putInt("nRecNo", nRecNo);
        args.putCharSequence("pictureFileSpec", pictureFileSpec);

        f.setArguments(args);
        return f;
    }
...
}

Sample code for getting the Bundle data in the same fragment ShowPictureFragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
       this.nRecNo = getArguments().getInt("nRecNo");
       this.pictureFileSpec = getArguments().getCharSequence("pictureFileSpec");
    }
...
}

Note: I get the Bundle data in onCreate, logical as anywhere else. Optionally, you may add a public method to get the Bundle data for the caller to use. But I never had to do that.

The Original Android
  • 6,147
  • 3
  • 26
  • 31
  • I'm still not able to get this working...let me give more information: – Anthony2000 Jun 02 '15 at 13:48
  • I'm still not able to get this working...let me give more information. I created the ShowPictureActivity by right clicking on the layout folder and selecting new-Actvity-Blank Activity with Fragment. This results in several files being created. The activity's method OnCreate calls setContentView(R.layout.activity_show_picture); When this method is called it is in turn creating the fragement. Where do I call the newInstance method? – Anthony2000 Jun 02 '15 at 14:04
  • @Anthony2000. I think you can call newInstance() in OnCreate() or when the user expects to see the new Fragment with the GUI. Technically you call newInstance anytime before you need to show the Fragment ShowPictureFragment. BTW, It does not matter to me how you create ShowPictureActivity. In that activity, you can call newInstance and use FragmentTransaction to show the Fragment. – The Original Android Jun 02 '15 at 17:19
  • @Anthony2000, Do you need sample code on how to use FragmentTransaction? Your post does not say that you have a problem with that. – The Original Android Jun 02 '15 at 17:21
  • I think what I could really use and I have not been able to find, is an example with a MainActivity (a fragment is not necessary), and a TestActivity with a Fragment. The MainActivity should have a button to launch the TestActivity. The button will create an intent and pass an integer and a string variable (they can be literals). Ultimately, the parameters would end up in the TestFragment class. I am developing for OS level 15 and above so there is no need for compat library. But, the example can use it if it is easier to create. – Anthony2000 Jun 02 '15 at 18:31
  • None of the examples I have seen show the whole process. I am new to using fragments. – Anthony2000 Jun 02 '15 at 18:32
  • @Anthony2000, At least, you should try the code out. Add the suggested code, test it, get issues, and then post the updated code to your original post. From your original post, it seems you have many things working already but we all don't know or see your existing code to help out. The sample code I submitted for you is basically "copy as is" and I am using your existing code! For now, I am assuming you're not asking for the full code to work completely. That would be a LOT of work. – The Original Android Jun 02 '15 at 18:39
  • @Anthony2000, In my answer, I did mention a good webpage with many samples @ http://developer.android.com/reference/android/app/Fragment.html. Did you read the sample codes? – The Original Android Jun 02 '15 at 18:43