I am trying to pass a String from a fragment to an activity, not the parent of this fragment. The activity is started from the fragment. The problem is that when I try to get the string it returns null. I couldn't figure out why. Here is the code: Inside the fragment:
loginButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceType")
@Override
public void onClick(View v) {
String email = emailText.getText().toString();
String password = passwordText.getText().toString();
String organizationName = orgSpinner.getSelectedItem().toString();
System.out.println("username loges in for organization " + organizationName + " with email " + email + " and pass : " + password);
Intent intent = new Intent(getActivity(), HomePageActivity.class);
GetUserTask task = new GetUserTask();
task.execute(1);
Bundle extras = new Bundle();
extras.putString("WELCOME_MSG", welcomeMsg);
//intent.putExtra("WELCOME_MSG", welcomeMsg);
System.out.println("!!"+ extras.getString("WELCOME_MSG"));
intent.putExtras(extras);
startActivity(intent);
}
});
return view;
}
And in the HomePageActivity I am trying to read the String:
Bundle extras = getIntent().getExtras();
if (extras != null) {
//String msg = getIntent().getStringExtra("WELCOME_MSG");
String msg = extras.getString("WELCOME_MSG");
System.out.println("HEREEE : " + msg);
welcomeMsg.setText(msg);
} else {
System.out.println("No extras");
}
From both sout calls (from fragment and from activity) I get null. I hope someone can figure why I this. Thank you.