简体   繁体   中英

How to remain signed in even when the user closes the app in android studio

In the start I had the problem in logging out (link: How to logout a user when he wants to in android studio ) . I removed the part which helped to keep the user signedin even after the app is closed and now the logout is working perfectly .But now my question is how should i keep the user logged in? this is my login page

public class Login_Page extends AppCompatActivity {
    ActivityLoginPageBinding binding;
    ProgressDialog progressdialog;
    FirebaseAuth auth;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginPageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        button = findViewById(R.id.Googlelogin);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login_Page.this, SignUp.class);
                startActivity(intent);
            } //taking the user to signup page
        });
        auth = FirebaseAuth.getInstance();

            progressdialog = new ProgressDialog(Login_Page.this);
            progressdialog.setTitle("Logging in");
            progressdialog.setMessage("Please wait");
            binding.Login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    progressdialog.show();
                    auth.signInWithEmailAndPassword(binding.Email.getText().toString(), binding.Password.getText().toString())
                            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    progressdialog.dismiss();
                                    if (task.isSuccessful()) {
                                        Toast.makeText(Login_Page.this, "Success", Toast.LENGTH_SHORT).show();
                                        String uid = auth.getInstance().getCurrentUser().getUid();
                                        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                                        DatabaseReference uidRef = rootRef.child("Users").child("Driver").child(uid);
                                        ValueEventListener valueEventListener = new ValueEventListener() {
                                            @Override
                                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                if (snapshot.exists()) {
                                                    Toast.makeText(Login_Page.this, "LoginDriver", Toast.LENGTH_SHORT);
                                                    Intent intent = new Intent(Login_Page.this, HomePage.class);
                                                    startActivity(intent);
                                                } else {
                                                    startActivity(new Intent(Login_Page.this, Custhomepage.class));
                                                }
                                            }

                                            @Override
                                            public void onCancelled(@NonNull DatabaseError error) {

                                            }
                                        };
                                        uidRef.addListenerForSingleValueEvent(valueEventListener);
                                    } else {
                                        Toast.makeText(Login_Page.this, task.getException().getMessage(), Toast.LENGTH_SHORT);
                                    }
                                }
                            });

                }
            });


        }
    }

I guess you are not checking whether the user is logged in or not. In activity's onCreate, call the below method checkFirebaseUser()

private void checkFirebaseUser () {
   FirebaseUser firebaseUser = mAuth.getCurrentUser ();
   if (firebaseUser != null) {
      //User is logged in already. You can proceed with your next screen
   } else {
      //User not logged in. So show email and password edit text for user to enter credentials
  }
}

Hope the above code snippet helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM