Aek*_*xit 3 java android android-xml android-activity
我有两项活动。
我也有一个带有此设置的启动屏幕:
<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>
Run Code Online (Sandbox Code Playgroud)
然后,启动屏幕使用以下代码将用户转移到主屏幕:
Intent intent = new Intent(SplashScreen.this, HomeScreen.class);
Run Code Online (Sandbox Code Playgroud)
在主屏幕活动中,我检查用户是否已经登录。如果他没有登录,我将他转移到 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);
}
}
};
Run Code Online (Sandbox Code Playgroud)
在 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);
}
}
};
Run Code Online (Sandbox Code Playgroud)
并且:
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();
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是:当我打开应用程序时,我会看到一个启动屏幕,然后转到登录页面。然后,当我按回键时,我会再次进入登录页面,而不是退出应用程序。另外,一旦我登录:我会看到启动屏幕,然后进入主屏幕。然后,如果我按返回,我会再次进入主屏幕,而不是退出应用程序。
请帮帮我。我搜索了 StackOverflow,但没有找到任何地方。任何帮助表示赞赏。如果您需要更多信息,请向我询问更新!:)
好的,所以我找到了解决方案。除了下面的宝贵回复之外,还需要在意图中添加一个标志: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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3531 次 |
| 最近记录: |