Android,请检查是否在其他活动中先按下了后退按钮

问题描述 投票:0回答:2

我已经开始使用Firebase,并进行了一个简单的活动来创建帐户并登录。我都按预期工作,但是,一旦用户登录并进入菜单屏幕,我就会注意到一些问题。如果用户按下了android设备上的后退按钮,则可以预期该应用程序将关闭或返回到您之前的位置,但是由于LoginActivity中onStart()的功能,它使用户再次回到菜单页面。我不想更改onStart()方法,因为如果您已经有一个帐户,它会记录您的身份。因此,我试图在菜单活动中按下后退按钮时关闭应用程序,如下所示:

@Override
    public void onBackPressed() {
        finish();
        System.exit(0);
    }

但是,这无法正常工作,我仍然无法关闭应用程序,然后在菜单活动中按返回按钮,因为LoginActivity仍然使我返回。

这是LoginActivity:

public class LoginActivity extends AppCompatActivity {

    // [START declare_auth]
    private FirebaseAuth mAuth;
    // [END declare_auth]

    private static final String TAG = "EmailPassword";

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

        // [START initialize_auth]
        // Initialize Firebase Auth
        mAuth = FirebaseAuth.getInstance();
        // [END initialize_auth]

    }

    //here we immediately check if user is already logged in then log in
    @Override
    protected void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if (currentUser != null)
            updateUI(currentUser);
    }

    private void updateUI(FirebaseUser currentUser) {
        Intent mainIntent = new Intent(LoginActivity.this, MenuActivity.class);
        Toast.makeText(this, "This is current user: " + currentUser, Toast.LENGTH_LONG).show();
        startActivity(mainIntent);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }


    public void createAccount(View view) {
        final EditText emailEdit = (EditText) findViewById(R.id.email_field);
        final String email = emailEdit.getText().toString();
        final EditText passwordEdit = (EditText) findViewById(R.id.password_field);
        final String password = passwordEdit.getText().toString();
        Log.d(TAG, "createAccount:" + email);

        // [START create_user_with_email]
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "createUserWithEmail:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            updateUI(user);
                        } else {

                            try
                            {
                                throw Objects.requireNonNull(task.getException());
                            }
                            // if user enters wrong email.
                            catch (FirebaseAuthWeakPasswordException weakPassword)
                            {
                                Log.d(TAG, "onComplete: weak_password");

                                Toast.makeText(LoginActivity.this, "Password to weak", Toast.LENGTH_LONG).show();
                            }
                            // if user enters wrong password.
                            catch (FirebaseAuthInvalidCredentialsException malformedEmail)
                            {
                                Log.d(TAG, "onComplete: malformed_email");

                                Toast.makeText(LoginActivity.this, "Wrong email", Toast.LENGTH_LONG).show();
                            }
                            catch (FirebaseAuthUserCollisionException existEmail)
                            {
                                Log.d(TAG, "onComplete: exist_email");

                                Toast.makeText(LoginActivity.this, "Email already exists", Toast.LENGTH_LONG).show();
                            }
                            catch (Exception e)
                            {
                                Log.d(TAG, "onComplete: " + e.getMessage());
                                Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT).show();
                                Toast.makeText(LoginActivity.this, "Please try again", Toast.LENGTH_SHORT).show();
                            }
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "createUserWithEmail:failure", task.getException());
                            Toast.makeText(LoginActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }

                    }
                });
        // [END create_user_with_email]
    }

}


是否有一种方法可以在菜单活动中关闭该应用程序,或者是否有一种方法可以通过按电话上的后退按钮在LoginActivity中检查onStart()方法?

android firebase android-activity firebase-authentication
2个回答
0
投票

您添加了这个吗?

<activity
   android:name=".LoginActivity"
   android:noHistory="true" />

0
投票

您可以修改意图添加标记Intent.FLAG_ACTIVITY_CLEAR_TASKIntent.FLAG_ACTIVITY_NEW_TASK

© www.soinside.com 2019 - 2024. All rights reserved.