【发布时间】:2017-06-05 12:25:44
【问题描述】:
我有两个活动。
- 登录活动
- 主屏幕
我也有这个设置的启动画面:
<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>
然后启动屏幕使用以下代码将用户转移到主屏幕:
Intent intent = new Intent(SplashScreen.this, HomeScreen.class);
在 HomeScreen Activity 中,我检查用户是否已经登录。如果他没有登录,我将他转移到 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 上,我有一个使用 FireBase Google 身份验证的简单登录按钮。另外,我检查用户是否已经登录,如果已经登录,则将他转移到 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,但没有完全到达任何地方。任何帮助表示赞赏。如果您需要更多信息,请向我询问更新! :)
【问题讨论】:
-
确保您在打开登录活动时
finish()您的启动画面活动。因为你想从后面的堆栈中删除它。 -
完成下面的活动 Intent intent = new Intent(SplashScreen.this, HomeScreen.class);完成();
-
我应该在 startActivity() 之后还是之前做?抱歉,我是 Android 新手。
-
在 startActivity() 之后尝试一次
-
在 startactivity 之后你应该调用 this.finish();
标签: java android android-activity android-xml