简体   繁体   English

登录成功后如何 go 回到 MainActivity?

[英]How do I go back to MainActivity after login successful?

After login successful this returns to Main2Activity and I can log in again and again.but start app again after closing and removing from recent list it start in MainActivity.登录成功后,这将返回到 Main2Activity,我可以一次又一次地登录。但是在关闭并从最近列表中删除后再次启动应用程序,它在 MainActivity 中启动。 how to directly navigate Main2Activity to MainActivity after if condition is true.如果条件为真,如何直接将 Main2Activity 导航到 MainActivity。

Main2Activity code 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() );
                }

            }
        }
    }
}

code- Main Activity代码-主要活动

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());
                    }
                });
    }
}

I will suggest you that you create a splash screen (startup screen) which will initially on start check for the authentication whether user is logged in or not from the server.我建议您创建一个启动屏幕(启动屏幕),该屏幕最初将在启动时检查用户是否从服务器登录。 All this will be handled in a background thread and the ui thread will carry on the splash screen animation(if you wish any).So if the user is logged in, you navigate to mainActivity else navigate to loginActivity.所有这些都将在后台线程中处理,并且 ui 线程将进行启动屏幕动画(如果您愿意的话)。因此,如果用户已登录,则导航到 mainActivity 否则导航到 loginActivity。 Hope this helps you.希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在Android中注册成功,我如何进入登录活动? - How do I go to the login activity if registration is successful in Android? 我如何从 MainActivity 的弹出登录转到另一个活动 - how do i go from pop up login of MainActivity to another activity 在回答几个问题后,我如何 go 返回菜单? - How do I go back to the menu after taking several questions? 如果按下浏览器后退按钮,如何防止在Spring MVC中成功登录后返回登录表单? - How to prevent going back to login form after successful login in Spring MVC if browser back button being pressed? 登录成功后将数据反馈给Android - Feeding data back to Android after login is successful 成功登录后如何获取用户名? - How can I get the username after successful login? 如何在每天成功登录后为用户添加积分 - How can i add points for user after successful login daily 单击后退按钮时如何返回主活动? - How to back to mainactivity when I click on backbutton? 使用Spring Security成功登录后,如何将用户转发回所需的受保护页面 - How to forward User back to desired protected page after successful login with Spring Security 从webview片段导航到不同的片段后,如何在webview中返回? - How do I go back within webview after navigation to a different fragment from the webview fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM