ROM
ROM

Reputation: 153

SharedPreferences not deleting values immediately

I store the user id in the SharedPreferences when the user logged in and I check when the user open the app if the id in the SharedPreferences is not null so the app will open its main screen without asking the user to log in again. Also, when the user logged out I delete his id from SharedPreferences. However, when the user logged out and try to open the app from the recent apps the app open its main screen without asking the user to log in again. but when the user presses back and reopens the app, the app will ask the user to log in again, so I am sure that the SharedPreferences is deleted successfully, but I don't know what is the problem with the recent apps list!

Here is my code:

Splash screen:

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferencesManager prefs = SharedPreferencesManager.getInstance(getApplicationContext());

        Intent intent;

        if (prefs.getUserId() != null) {

            intent = new Intent(SplashActivity.this, mainscreen.class);

        } else {

            intent = new Intent(SplashActivity.this, SignIn.class);
        }

        startActivity(intent);

        finish();

    }
}

SharedPreferencesManager:

public class SharedPreferencesManager {

    private static SharedPreferencesManager sharedPreferencesManager = new SharedPreferencesManager();
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;
    private static final String NAME = "General";

    public static SharedPreferencesManager getInstance(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(NAME, Activity.MODE_PRIVATE);
            editor = sharedPreferences.edit();
        }
        return sharedPreferencesManager;
    }

    public void setUserId(String ID){
        editor.putString("UserID", ID).commit();
    }


    public String getUserId(){
        return sharedPreferences.getString("UserID", null);
    }


    public void removeUserId(){
         editor.remove("UserID").commit();
    }

}

logout:

public void logout(){

        SharedPreferencesManager prefs = SharedPreferencesManager.getInstance(getApplicationContext());

        prefs.removeUserId();
        Intent intent = new Intent(this, SignIn.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

Thank you!

Upvotes: 0

Views: 54

Answers (2)

Soroosh
Soroosh

Reputation: 755

you should override onResume method in your Main activity like this:

@Override
public void onResume(){
    super.onResume();

SharedPreferencesManager prefs = SharedPreferencesManager.getInstance(getApplicationContext());

        Intent intent;

        if (prefs.getUserId() == null) {

            intent = new Intent(MainActivity.this, SignIn.class);
        }

        startActivity(intent);

}

Upvotes: 3

Nouman Ch
Nouman Ch

Reputation: 4121

Instead of removing value set the value to null.

     public void removeUserId(){
        editor.putString("UserID", null).commit();
    }

Hope this will work for you.

Upvotes: 0

Related Questions