Sharedpreferences - Firebase android

Asked

Viewed 160 times

0

I am logging in normally but quado close application, back to 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 {

                    }
                }
            });
}

2 answers

0

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

If the answer is YES, do one more:

editor.putBoolean("permanecer", false);

And then in the onCreate of your Login screen, you check if this value ta as true, if you already enter the main screen, otherwise you Fican the login screen

  • And if the answer is no(I don’t have a checkbox field to stay logged in), you can use the same schema, only there in your application, on the main screen, it would be interesting to have a user option to drop OR quit

0

You have to save the received value when you enter 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));
}
  • Even so the same thing is happening, back to login screen. So I have to get logged in user to be able to save in sharedPreferences.

  • @Douglaswilliam was made confusion in the condition; See

  • @Douglaswilliam made a change. Take a look... you have even saved the data when the user exists and not when it does not exist.

  • Even so the same thing is happening, back to login screen

  • @Douglaswilliam debugs his code and see how the user is getting, if he really is saving the data in this variable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.