Android - sharedpreferences returns null

I am trying to use sharedpreferences to check if a user is logged in before they start using the application. I save the username in shredpreferences when the user logs in.

Login.java

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); Button btnLogin = (Button) findViewById(R.id.buttonlogin); btnLogin.setOnClickListener(new View.OnClickListener() { public void onClick(View adapt) { EditText usernameEditText = (EditText) findViewById(R.id.EditUserName); userName = usernameEditText.getText().toString(); EditText passwordEditText = (EditText) findViewById(R.id.EditPassword); userPassword = passwordEditText.getText().toString(); if (userName.matches("") && userPassword.matches("")) { toast("Please enter Username and Password"); return; } else if (userName.matches("") || userName.equals("")) { toast("Please enter Username"); return; } else if (userPassword.matches("") || userPassword.equals("")) { toast("Please enter Password"); return; } else { SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("MEM1", userName); editor.commit(); new DownloadFilesTask().execute(); } } }); } private void toast(String text) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); } private class DownloadFilesTask extends AsyncTask<Void, Void, Void> { protected void onPreExecute() { } protected void onPostExecute(Void result) { toast("user logged in"); startActivity(new Intent(Login.this, MainActivity.class)); finish(); } @Override protected Void doInBackground(Void... params) { return null; } } } 

and I tried to check the username value before starting my mainactivity.

 SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); String username =sharedPreferences.getString("MEM1", ""); if(username.equalsIgnoreCase("")||username.length()==0) { toast("username is null"); startActivity(new Intent(MainActivity.this, Login.class)); finish(); } 

but the username is always null. Please help. Thanks

+6
source share
3 answers

Write the code below instead of the code to save the username in sharedpreferences.

 SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); SharedPreferences.Editor editor = myPrefs.edit(); editor.putString("MEM1", userName); editor.commit(); 

And use the code below to get preference values.

 SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); String username = myPrefs.getString("MEM1",""); 
+9
source

Calling getPreferences means you must be in one action. Using getSharedPreferences allows getSharedPreferences to share preferences between actions. You just need to define a name for preferences when calling getSharedPreferences("Pref name here", MODE_PRIVATE);

+17
source
 private final String TAXI_SPREF = "TAXI_SHARED_PREFERENCES"; //set-save data to shared preferences SharedPreferences.Editor sPEditor = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE).edit(); sPEditor.putInt("USERID", etUserID.getText().toString()); sPEditor.putString("EMAIL", etEmail.getText().toString()); sPEditor.apply(); //get data from shared preferences SharedPreferences sharedPreferences = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE); int userID = sharedPreferences.getInt("USERID", 0); string email = sharedPreferences.getString("EMAIL", 0); //////// or ///// String email = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE).getString("EMAIL","EMPTY"); if (!email.equals("EMPTY")){ etEmail.setText(email); } 
+1
source

Source: https://habr.com/ru/post/925742/


All Articles