Values in sharedpreferences are not cleared

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();

                }
            });

}
}

Answers


In your code:

//prefs.commit();

Change this. Remove it from the comments.

prefs.commit();

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.


You have commented out the calls to prefs.commit(). Uncomment them.


Need Your Help

How to retrieve a parameter from DownloadDataAsync?

c# asp.net wpf webclient

Using WebClient in WPF app, the following code works fine, when an image is downloaded an event fire correctly.

Android big text notification

android android-notifications

I trying to use big text style with NotificationCompat.Builder it's work fine in android 4.4.4 but i have a problem in Android 4.0.3 - my notification is not expanded