0

I want to pass multiple values from my fragment to the other fragment.

This is what i do in fragment A:

mCallback.passData("title","test");

Activity:

Activity

Fragment B:

Bundle args = getArguments();
    if(args != null){
        Toast.makeText(getActivity(), args.getString(NAME_RECEIVE), Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity(), args.getString(TITLE_RECEIVE), Toast.LENGTH_LONG).show();
    }

But i only get one argument from my bundle

Thanks @cricket_007

I was passing 2 times the same string for NAME_RECEIVE and TITLE_RECEIVE:

final static String TITLE_RECEIVE = "data_receive";
final static String NAME_RECEIVE = "data_receive";
Joris
  • 119
  • 9
  • 4
    what is `NAME_RECEIVE ` and `TITEL_RECEIVE `, they should not be the same – WenChao Feb 14 '17 at 22:16
  • That's not the best way the instantiate a fragment. You should be probably using the static `newInstance(String arg1, String arg2)` function. More info here: http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – azurh Feb 14 '17 at 22:21
  • Might want to fix your TITEL typo – OneCricketeer Feb 14 '17 at 22:29

1 Answers1

6

Your Strings are the same.

Look at your debugger, and you see Bundle[{data_received="test"}], meaning that NAME_RECEIVE is the exact same value of TITEL_RECEIVE, which is "data_received"

Also, never Toast two things at once. You'll only see one pop-up box.

Try Log instead.

Bundle args = getArguments();
if(args != null){
    Log.d(NAME_RECEIVE, args.getString(NAME_RECEIVE));
    Log.d(TITEL_RECEIVE, args.getString(TITEL_RECEIVE));
}

Additional reference for "proper" Fragment creation: Best practice for instantiating a new Android Fragment

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Thanks @cricket_007. Indeed i was using this in fragment B: final static String TITLE_RECEIVE = "data_receive"; final static String NAME_RECEIVE = "data_receive"; – Joris Feb 14 '17 at 22:27