Not able to clear shared preferences

593 views Asked by At

I made a simple login activity in which I used android sqlite database to store login information i.e. username and password. When user click create account button in signup activity, s/he will be transferred to another activity where they can see what is their username and pass. In that screen I need to check if preferences are clear and then the text of the button will be changed based on the decision.

I passed the info between activities using SharedPreference, but I can not delete it or clear it when the user clicks sign out.. Below is the code of both activities..

Here's my SignUp.java

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);

    // get Instance  of Database Adapter
    loginDataBaseAdapter=new LoginDataBaseAdapter(this);
    loginDataBaseAdapter=loginDataBaseAdapter.open();

    // Get Refferences of Views
    editTextUserName=(EditText)findViewById(R.id.editTextUserName);
    editTextPassword=(EditText)findViewById(R.id.editTextPassword);
    editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
    tv=(TextView)findViewById(R.id.textView);

    btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
    btnCreateAccount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String userName=editTextUserName.getText().toString();
            String password=editTextPassword.getText().toString();
            String confirmPassword=editTextConfirmPassword.getText().toString();

            // check if any of the fields are vaccant
            if(userName.equals("")||password.equals("")||confirmPassword.equals("")) {
                Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                return;
            }
            // check if both password matches
            if(!password.equals(confirmPassword)) {
                Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
                return;
            }
            if(password.length()<8) {
                Toast.makeText(getApplicationContext(),"Password must be 8 digits longer",Toast.LENGTH_LONG).show();
            }
            else {
                // Save the Data in Database
                loginDataBaseAdapter.insertEntry(userName, password);
                try {

                    sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(Name,userName);
                    editor.putString(Pass,password);
                    editor.commit();

                } catch (NullPointerException e) {
                    e.printStackTrace();
                }

                intent=new Intent(getApplicationContext(),AfterLogin.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
            }
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    loginDataBaseAdapter.close();
}

And the AfterLogin.Java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.afterlogin);

    username=(TextView)findViewById(R.id.Username);
    name=(TextView)findViewById(R.id.Name);
    user=(TextView)findViewById(R.id.User);
    pass=(TextView)findViewById(R.id.Pass);
    sout=(Button)findViewById(R.id.SignOut);

    s= sharedPreferences.getString(Name,"");
    us=sharedPreferences.getString(Pass,"");

    username.setText(s);
    name.setText(s);
    user.setText(s);
    pass.setText(us);

    sout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.clear();
            editor.commit();

            String n=sharedPreferences.getString(Name,"");

            if (n == null) {
                sout.setText("Signed Out");
            }
        }
    });
}
3

There are 3 answers

0
ShekharKG On BEST ANSWER

AfterLogin.java you have checked

n == null

but passed the default value as empty for String n

String n=sharedPreferences.getString(Name,""); //EMPTY

change

if (n==null) {
    sout.setText("Signed Out");
}

to

if (n.isEmpty()) {
    sout.setText("Signed Out");
}
0
Febi M Felix On

Can you try by committing the clear and commit in a single line. Like editor.clear().commit() or editor.clear().apply(). This might help you since you are committing or applying changes on editor object after making changes.

0
Hugo On

The problem could be that you are using String n=sharedPreferences.getString(Name,""); which always returns something. Default value if preference doesn't exists or it's value if it exists.

To check if a preference has been deleted i'd better use String n=sharedPreferences.contains(Name); which returns a boolean that shows if the preference exists or not.

Hope it helps!