登录成功后,这将返回Main2Activity,我可以一次又一次登录。但是,关闭并从最近的列表中删除后,应重新启动应用程序,并从MainActivity中启动。如果条件为真,如何直接将Main2Activity导航到MainActivity。
Main2Activity代码
public class Main2Activity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
int AUTHUI_REQUEST_CODE = 1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
startActivity(new Intent(this, MainActivity.class));
this.finish();
}
}
public void loginRegister(View view) {
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build(),
new AuthUI.IdpConfig.PhoneBuilder().build()
);
Intent intent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.setTosAndPrivacyPolicyUrls("https://example.com","https://example.com")
.setLogo(R.drawable.i789)
.build();
startActivityForResult(intent, AUTHUI_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AUTHUI_REQUEST_CODE){
if (requestCode == RESULT_OK){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Log.d(TAG, "onActivityResult: "+ user.getEmail());
if (user.getMetadata().getCreationTimestamp() == user.getMetadata().getLastSignInTimestamp()){
Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Welcome back again", Toast.LENGTH_SHORT).show();
}
Intent t = new Intent(this,MainActivity.class);
startActivity(t);
this.finish();
}else{
IdpResponse response =IdpResponse.fromResultIntent(data);
if (response == null){
Log.d(TAG, "onActivityResult: the user has cancelled the sign in request");
}else {
Log.e(TAG, "onActivityResult: ",response.getError() );
}
}
}
}
}
代码-主要活动
public class MainActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void startLogin(){
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
finish();
}
public void signout(View view){
AuthUI.getInstance().signOut(this);
}
@Override
protected void onStart() {
super.onStart();
FirebaseAuth.getInstance().addAuthStateListener(this);
}
@Override
protected void onStop() {
super.onStop();
FirebaseAuth.getInstance().removeAuthStateListener(this);
}
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
startLogin();
return;
}
firebaseAuth.getCurrentUser().getIdToken(true)
.addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult getTokenResult) {
Log.d(TAG, "onSuccess: "+getTokenResult.getToken());
}
});
}
}
答案 0 :(得分:1)
我建议您创建一个启动屏幕(启动屏幕),该屏幕最初会在启动时首先检查身份验证,无论用户是否从服务器登录。所有这些将在后台线程中处理,并且ui线程将进行启动屏幕动画(如果您愿意的话)。因此,如果用户已登录,则导航到mainActivity,或者导航到loginActivity。希望对您有帮助。