按下返回按钮显示相同的活动 [英] Pressing back button shows same activity

查看:74
本文介绍了按下返回按钮显示相同的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动.

  1. LoginActivity
  2. 主屏幕

我还具有以下设置的启动屏幕:

I also have a splash screen with this setting:

<activity android:name=".SplashScreen"
            android:theme="@style/AppTheme.NoActionBar"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

然后,启动屏幕通过使用以下代码将用户转移到主屏幕:

The Splash Screen then transfers the user to the HomeScreen by using this code:

Intent intent = new Intent(SplashScreen.this, HomeScreen.class);

在HomeScreen活动中,我检查用户是否已经登录.如果他尚未登录,我会将他转移到LoginActivity:

Inside the HomeScreen Activity, I check if the user is already signed in or not. If he's not signed in, I transfer him to the LoginActivity:

 mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
                if(firebaseAuth.getCurrentUser() == null)
                {
                    Intent intent = new Intent(HomeScreen.this,LoginActivity.class);
                    Toast.makeText(HomeScreen.this,"H>L on no login",Toast.LENGTH_LONG).show(); //toast to show me why my app is in infinite loop
                    startActivity(intent);
                }
            }
        };

在LoginActivity上,我有一个简单的Login按钮,该按钮使用FireBase Google身份验证.此外,我还会检查用户是否已经登录,如果已经登录,则将其转移到HomeScreen活动中:

On the LoginActivity, I have a simple Login button which uses FireBase Google Authentication. Also, I check if the user is already signed in or not, and if he is, he's transferred to HomeScreen Activity:

mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if(firebaseAuth.getCurrentUser() != null)
                {
                    Intent intent = new Intent(LoginActivity.this,HomeScreen.class);
                    Toast.makeText(LoginActivity.this,"L>H already signed in",Toast.LENGTH_LONG).show();
                    startActivity(intent);
                }
            }
        };

而且:

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Intent intent = new Intent(LoginActivity.this,HomeScreen.class);
                            Toast.makeText(LoginActivity.this,"L>H on login",Toast.LENGTH_LONG).show();
                            startActivity(intent);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(LoginActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

现在,问题是:打开应用程序时,出现启动画面,并转移到登录"页面.然后,当我按返回时,我会再次获得登录"页面,而不是退出应用程序.另外,一旦我被唱歌,我就会看到启动画面,然后被带到主画面.然后,如果我按回去,则会再次进入HomeScreen,而不是退出应用程序.

请帮帮我.我搜索了StackOverflow,但没有到达任何地方.任何帮助表示赞赏.如果需要更多信息,请询问我! :)

Please help me out. I searched StackOverflow, but didn't quite reach anywhere. Any help is appreciated. Ask me for updates if you need more info! :)

推荐答案

好的,所以我找到了解决方案.除了以下宝贵的答复外,还必须在意图上添加一个标记:FLAG_ACTIVITY_CLEAR_TOP.

Okay, So I found a solution. Apart from the precious replies below, it was also necessary to add a flag to the intent saying FLAG_ACTIVITY_CLEAR_TOP.

这是工作代码:

public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Intent intent = new Intent(LoginActivity.this,HomeScreen.class);
                        Toast.makeText(LoginActivity.this,"L>H on login",Toast.LENGTH_LONG).show();
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                        startActivity(intent);
                        finish();
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(LoginActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                }

这篇关于按下返回按钮显示相同的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆