SharedPreferences - Firebase android

0

I am logging in normally, but when I close the application, it returns to the login screen. I'm trying to run sharedPreferences but I'm not successful.

public static final String PREF_NAME = "LoginActivityPreferences";
private SharedPreferences mPreferences;

// Metodo onCreate
FirebaseApp.initializeApp(LoginActivity.this);
mAuth = FirebaseAuth.getInstance();

// Metodo shared
private void sharedPreferences(String email, String password) {


    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        mPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);

            if (user != null) {
                startActivity(new Intent(getApplicationContext(), DashboardActivity.class));

            } else {
                SharedPreferences.Editor editor = mPreferences.edit();
                editor.putString("email", email);
                editor.putString("password", password);
                editor.commit();
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
            }
}

 // Metodo de login email e senha
 private void singInEmailSenha(final String email, final String password) {
    mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        sharedPreferences(email, password);
                    } else {

                    }
                }
            });
}
    
asked by anonymous 05.09.2017 / 00:58

2 answers

0

You have to save the received value when it enters the condition if the user exists. See:

mPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
if (user != null) {

    SharedPreferences.Editor editor = mPreferences.edit();
    editor.putString("email", email);
    editor.putString("password", password);
    editor.commit();

    startActivity(new Intent(getApplicationContext(), DashboardActivity.class));

} else {

    startActivity(new Intent(getApplicationContext(), LoginActivity.class));
}
    
05.09.2017 / 14:56
0

Do you have any "Keep me logged in" field?

If the answer is YES, do one more:

editor.putBoolean("permanecer", false);

And in the onCreate of your Login screen, you check if this value is true, if you have already entered the main screen, otherwise, you get the login screen

    
06.09.2017 / 02:13