Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logout clear SharedPreferences

Tags:

I have a login page that saves username and password to SharedPreferences. I have another Activity class that includes a logout button. I want to clear SharedPreferences when I click the logout button. The problem is that I don't get the SharedPreferences from this class. How can I get the SharedPreferences?

LoginPage

public class MainActivity extends Activity { public SharedPreferences.Editor loginPrefsEditor;     public  SharedPreferences loginPreferences;     private Boolean saveLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);      name = (EditText) findViewById(R.id.et_Username);         pass = (EditText) findViewById(R.id.et_Password);         login = (Button) findViewById(R.id.bt_Login);           loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);          loginPrefsEditor = loginPreferences.edit();           saveLogin = loginPreferences.getBoolean("saveLogin", false);           if (saveLogin == true) {                 name.setText(loginPreferences.getString("username", ""));                 pass.setText(loginPreferences.getString("password", ""));                }          login.setOnClickListener(new OnClickListener()         {             @Override             public void onClick(View v) {                  name1 = name.getText().toString();                  pass1 = pass.getText().toString();                //new Thread (new Task()).start();                     loginPrefsEditor.putBoolean("saveLogin", true);                     loginPrefsEditor.putString("username", name1);                     loginPrefsEditor.putString("password", pass1);                     loginPrefsEditor.commit();                  new myAsyncTask().execute();             }         }); } 

Logout Button in AnotherActivity

 logout.setOnClickListener(new View.OnClickListener() {              @Override             public void onClick(View view) {                 // Launching News Feed Screen                  Intent i = new Intent(getApplicationContext(), MainActivity.class);                 startActivity(i);             }         }); 
like image 591
HurkanSeyhan Avatar asked Jun 11 '14 13:06

HurkanSeyhan


People also ask

How do I clear all SharedPreferences in flutter?

You can simply use clear() function with your variable it will clear all the shared preferences. SharedPreferences preferences = await SharedPreferences. getInstance(); await preferences. clear();

How do I delete shared preferences?

Clearing shared preferences To clear all the values in the shared preferences file, call the clear() method on the shared preferences editor and apply the changes. SharedPreferences.

How do you delete preferences on Android?

You use remove() to remove specific preferences, you use clear() to remove them all.


2 Answers

Try this !

logout.setOnClickListener(new View.OnClickListener() {          @Override         public void onClick(View view) {             // Launching News Feed Screen               SharedPreferences preferences =getSharedPreferences("loginPrefs",Context.MODE_PRIVATE);              SharedPreferences.Editor editor = preferences.edit();              editor.clear();              editor.apply();              finish();  }); 
like image 90
Krupa Patel Avatar answered Sep 28 '22 06:09

Krupa Patel


I think you have a trouble in understanding Shared preferences in android .

According to official documentation

To get a SharedPreferences object for your application, use one of two methods:

getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

You should have a Context for using both the above methods .

Also Shared preferences are stored asa key value pair , so clearing should mean that you set the values to some empty string.

For more details , and better explanation you can read here http://developer.android.com/guide/topics/data/data-storage.html#pref and http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html

Hope this will help.

Cheers!

like image 31
Shubhang Malviya Avatar answered Sep 28 '22 05:09

Shubhang Malviya