So I have a SharedPreference I’m calling logged_in. I get the preference and check if it’s 1, if it is it’s supposed to start the new activity, otherwise I want it to display to me what it actually is. Here’s the code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
prefs = getSharedPreferences("preferences", MODE_PRIVATE);
// Check if the user is logged in
String loggedIn = prefs.getString("logged_in", null);
if(loggedIn != "1") {
setContentView(R.layout.main);
TextView textView = (TextView) findViewById(R.id.login_status);
textView.setText(prefs.getString("logged_in", null));
}
else {
startActivity(new Intent(this, LoggedInActivity.class));
}
}
What’s really weird is that it will load the main view instead of starting the LoggedIn Activity, but then it displays the logged_in preference as being a 1. So according to what I’m seeing, it should be starting the new activity, but it’s not. I’m really confused on this. Any help is greatly appreciated.
You are comparing references instead of string values. That is, you are saying do these two references point to the same object instead of are the content of this two objects the same. You should do instead:
Note however that if
loggedInisnull, that code will raise aNullPointerException. So this would be more appropriate:That said, in this particular case you would be better off by using an
intpreference.