I am developing an app in which I need to manage users session i.e when user logs in for the first time he must see the login page and once he is authenticated he is redirected to the home screen and tht time a value is set in sharedpreferences. Now on the home screen when the user clicks on logout button the values in shsredpreferences must be cleared and the next time the user opens the app he must be directed to login page.Unless and until user clicks logout he must not be shown the login page.
I am able to store values in sharedpreferences but not able to delete them.
here is my code for loginpage.java
package com.sess.eg;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class loginpage extends Activity {
/** Called when the activity is first created. */
EditText acc,user,pin;
Button login;
StringBuilder builder = new StringBuilder();
String UserName;
SharedPreferences.Editor prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// SharedPreferences prefs1 = getPreferences(MODE_WORLD_READABLE);
// UserName= prefs1.getString("User", "Abcdef");
//
// System.out.println(UserName);
//
// if(UserName.equals("Ad"))
//
// {
// System.out.println(UserName);
// Intent i=new Intent(loginpage.this,homepage.class);
// startActivity(i);
// }
setContentView(R.layout.main);
acc = (EditText) findViewById(R.id.ed_login_acc);
user = (EditText) findViewById(R.id.ed_user_acc);
pin = (EditText) findViewById(R.id.ed_pin_acc);
login = (Button) findViewById(R.id.login_button);
SharedPreferences prefs1 = getPreferences(MODE_WORLD_READABLE);
login.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
prefs.putString("User", "Ad");
prefs.commit();
//System.out.println(sendJson1());
//startService(new Intent(Login.this, MyService.class));
Intent i = new Intent(loginpage.this, homepage.class);
startActivity(i);
finish();
//System.out.println(UserName);
}
});
}
@Override
protected void onStart() {
SharedPreferences prefs1 = getPreferences(MODE_WORLD_READABLE);
UserName= prefs1.getString("User", "Abcdef");
System.out.println(UserName);
if(UserName.equals("Ad"))
{
System.out.println(UserName);
Intent i=new Intent(loginpage.this,homepage.class);
startActivity(i);
finish();
}
super.onStart();
}
}
here is my code for homepage.java
package com.sess.eg;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class homepage extends Activity{
Button logout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home3);
logout = (Button) findViewById(R.id.logout);
logout.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
SharedPreferences.Editor prefs = getPreferences(MODE_WORLD_WRITEABLE).edit();
prefs.clear();
// prefs.commit();
SharedPreferences prefs1 = getPreferences(MODE_WORLD_READABLE);
String UserName= prefs1.getString("User", "Abcdef");
System.out.println(UserName);
finish();
}
});
}
}
In your code:
Change this. Remove it from the comments.
commit()is called to save your preferences changes.“Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.
Note that when two editors are modifying preferences at the same time, the last one to call commit wins. “
Read here.