kennyYice23 kennyYice23 - 25 days ago 7
Java Question

Bundle.getString() doesn't seem to get the values

I want my MainActivity to get extras from two different activties and I figured the only way to differ from the two activties is to check the key of the Bundles but when i try to get the extras that they come from, they are all null.

Main Activity:

public class UserActivity extends AppCompatActivity {
public String token;
public String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Intent intent = getIntent();
Bundle b = intent.getExtras();
if(b.containsKey("createBundle"))
{
token = b.getString("sessionID");
name = b.getString("nameID");
}
else if(b.containsKey("logInBundle"))
{
token = b.getString("loginToken");
name = b.getString("loginName");
}

TextView welcome = (TextView) findViewById(R.id.welcomeView);
welcome.setText("Hello, " + name);
}


Activity 1:

public class CreateUserActivity extends Activity {
EditText txtName, txtEmail, txtPassword;
AlertDialogManager alert = new AlertDialogManager();
private String token;
private SharedPreferenceManager sharedPreferenceManager;

public void setToken(String token) {
this.token = token;
}

public String getToken() {
return token;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_user_activity);
txtName = (EditText) findViewById(R.id.loginName);
txtEmail = (EditText) findViewById(R.id.loginEmail);
txtPassword = (EditText) findViewById(R.id.loginPassword);
sharedPreferenceManager = new SharedPreferenceManager();

}

public void checkCreateUser(View view) throws JSONException {
String name = txtName.getText().toString();
String email = txtEmail.getText().toString();
String password = txtPassword.getText().toString();
String tokenAgain = "";

CheckBox checkBox = (CheckBox) findViewById(R.id.login_remember_checkbox);
if (name.trim().length() > 0 && password.trim().length() > 0 && email.trim().length() > 0) {
JSONObject userObj = new JSONObject();
userObj.put("name", name);
userObj.put("email", email);
userObj.put("password", password);
String jsonDocument = userObj.toString();
PostUserTask put = new PostUserTask();
put.execute("http://api.evang.dk/v2/users", jsonDocument);

tokenAgain = getToken();

if (checkBox.isChecked()) {
sharedPreferenceManager.saveData(CreateUserActivity.this, "USERNAME", name);
sharedPreferenceManager.saveData(CreateUserActivity.this, "EMAIL", email);
sharedPreferenceManager.saveData(CreateUserActivity.this, "PASSWORD", password);
sharedPreferenceManager.saveData(CreateUserActivity.this, "TOKEN", tokenAgain);
} else {
sharedPreferenceManager.removeData(CreateUserActivity.this, "USERNAME");
sharedPreferenceManager.removeData(CreateUserActivity.this, "EMAIL");
sharedPreferenceManager.removeData(CreateUserActivity.this, "PASSWORD");
sharedPreferenceManager.removeData(CreateUserActivity.this, "TOKEN");
}

}
else
{
alert.showAlertDialog(CreateUserActivity.this, "Login failed!", "Please enter name, username and password", false);
}
Intent i = new Intent(getBaseContext(), UserActivity.class);
Bundle b = new Bundle();
b.putString("sessionID", tokenAgain);
b.putString("nameID", name);
i.putExtra("createBundle", b);
startActivity(i);
}


Activity 2:

public class LoginActivity extends AppCompatActivity {
EditText userNameTxt, passwordTxt, emailTxt;
SharedPreferenceManager sharedPreferenceManager;
String userName;
String token;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Intent intent = getIntent();
userNameTxt = (EditText) findViewById(R.id.userNameLogin);
passwordTxt = (EditText) findViewById(R.id.passwordLogin);
emailTxt = (EditText) findViewById(R.id.emailLogin);
sharedPreferenceManager = new SharedPreferenceManager();
userName = sharedPreferenceManager.getData(this, "USERNAME");
String email = sharedPreferenceManager.getData(this, "EMAIL");
String password = sharedPreferenceManager.getData(this, "PASSWORD");
token = sharedPreferenceManager.getData(this, "TOKEN");
if(userName != null && email != null && password != null && token != null)
{
userNameTxt.setText(userName);
emailTxt.setText(email);
passwordTxt.setText(password);
}
}

public void onClickLog(View view) {
if(token != null)
{
Intent i = new Intent(getBaseContext(), UserActivity.class);
Bundle b = new Bundle();
b.putString("loginToken", token);
b.putString("loginName", userName);
i.putExtra("logInBundle", b);
startActivity(i);
}
}


}

Answer

Accordingly to the code you posted, the following

 if(b.containsKey("createBundle")) {
        token = b.getString("sessionID");
        name = b.getString("nameID");
  } else if(b.containsKey("logInBundle")) {
       token = b.getString("loginToken");
       name = b.getString("loginName");
  }

is wrong. onClickLog you are adding a new a Bundle with key logInBundle. What you should check with getIntent().hasExtra(String). E.g.

if(getIntent().hasExtra("createBundle")) { 
   b = getIntent().getBundleExtra("createBundle");
} else if (getIntent().hasExtra("logInBundle")) {
   b = getIntent().getBundleExtra("logInBundle");
}

and then read from b as you are already doing