Reputation: 548
I am tyring to add a fragment in a navigation Drawer and some of the data have to pass to the fragment,
but getArgument returns null and I still cannot solve the problem after reading the similar question.
In my drawer class, the fragment is added by
Bundle args = new Bundle();
args.putString("username",username);
args.putString("password",password);
UserLoginFragment alreadyLoginFragment = new UserLoginFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
alreadyLoginFragment.setArguments(args);
fragmentTransaction.add(R.id.drawer_User_Container, alreadyLoginFragment, "alreadyLogin");
fragmentTransaction.commit()
;
And in the fragment, the argument is get by :
String username;
String password;
public UserLoginFragment(){
Bundle args=getArguments();
if(args!=null){
Log.d("test","have args");
}
else{
Log.d("test","no args");
}
}
And the log will be no args
Upvotes: 0
Views: 226
Reputation: 1076
Dont call getArgument()
in the constructor, at the time the constructor is called, the argument bundle has not yet been set, so it will return null
every time no matter what you do. Call it like this
@Override
public void onAttach(Activity act)
{
Bundle args = getArguments();
}
Upvotes: 1
Reputation: 3181
You are just recieving the argument. You must recieve the content that was passed in the argument.
String username = getArguments().getString("username");
String password = getArguments().getString("password");
Upvotes: 0