'Store a variable in Android

Is there any way to have a boolean that goes true when successful login and false when not, and to be able to access it and change its value in the activities?

I've tried with abstract class, interface and declaring a global variable, but either I can't change their values nor keep its value changed between activities.

Is there any way to store it on shared preferences or something? Because what I've been using to store data in sharedPreferences is:

PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("LOGED", Boolean.TRUE).apply();

And when I try to retrieve data in other activity, this method always returns false as it asks me to gave a value:

PreferenceManager.getDefaultSharedPreferences(this).getBoolean("LOGED",Boolean.FALSE)

How could I store a boolean and get/set its value between activities?



Solution 1:[1]

I've managed to do it and work with it clearer by creating a class with constants and a preference manager class customised:

Constants class:

public class Constants {
    public static final String KEY_NAME = "name";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PASSWORD = "password";    
}

And the customised PreferenceManager like:

import android.content.Context;
import android.content.SharedPreferences;

public class PreferenceManager {

    private final SharedPreferences sharedPreferences;

    public PreferenceManager(Context context){
        sharedPreferences = context.getSharedPreferences(Constants.KEY_PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

    public void putBoolean(String key, Boolean value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public Boolean getBoolean(String key){
        return sharedPreferences.getBoolean(key, false);
    }

    public void putString(String key, String value){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public String getString(String key){
        return sharedPreferences.getString(key, null);
    }

    public void clear(){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.apply();
    }

}

All is left is work with it:

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private PreferenceManager preferenceManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        preferenceManager = new PreferenceManager(getApplicationContext());
    }


    private void loadUserDetails(){
        binding.textName.setText(preferenceManager.getString(Constants.KEY_NAME));
 
    }
}

Solution 2:[2]

The best way is you save data in a Shareprefence, and every time check that. for security, you can encrypt data.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alen
Solution 2 Rasoul Miri