Android Storage

0

I'm starting in the Android universe, and I have the following question:

In a login system, which storage type should I use to persist the data of the user who logged in to that session, and when I click on leave "kill" this session information?

    
asked by anonymous 11.01.2018 / 11:56

2 answers

0

There are some forms of persistence in Android. You can use ready-made authentication APIs (such as Facebook and Google), or simple database like SQLite own Android or even SharedPreferences.

An example would be, you authenticate and save the data in SharedPrefences and when the user exits you clear the preferences.

In this example I persisted the login data in preferences (which stays in the app) and when the user opens the app again I check if he has ever logged in and if he did, he does not need to perform again.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_login);

    preferences = getSharedPreferences(Contract.SETTINGS_PREF, 0);
    String name = preferences.getString(Contract.USER_NAME_PREF, "");
    String email = preferences.getString(Contract.USER_EMAIL_PREF, "");
    String password = preferences.getString(Contract.USER_PASSWORD_PREF, "");

    if (!email.isEmpty() && !password.isEmpty()) {
        logIn(email, password);
    }

    binding.txtCreateAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this, CreateAccountActivity.class));
        }
    });
    
11.01.2018 / 13:29
0

A good option is to use some web service to login and store your data and use sqlLite as a "super cache". Take a look at the firebase, it has great forms of authentication: by email or social network. In addition you can save your data and files. link

    
11.01.2018 / 20:47