Joris
Joris

Reputation: 119

Android fragment bundle only get one arg

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";

Upvotes: 0

Views: 106

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191844

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

Upvotes: 6

Related Questions