繁体   English   中英

如果已登录,请正确跳过登录活动

[英]Properly skip login activity if already logged in

我的启动器图标当前启动登录活动。 我已将登录状态存储在SharedPreferences中。 有没有办法正确跳过登录活动,直接进入主要活动,没有任何 UI故障。 所有涉及onCreate()中的finish()现有解决方案都会导致登录活动标题短暂可见或其他一些简短的空白屏幕UI故障。

有一个没有UI决定打开MainActivity或LoginActivity的启动器活动。 您可以声明没有用户界面:

android:theme="@android:style/Theme.NoDisplay"

另外两个可能的解决方

反过来说:将mainActivity作为启动器,并检查用户是否已登录。然后在不是这种情况下重定向到loginActivity。

另一种方法是使用片段。 有一个可以加载mainFragment和loginFragment的基本活动。 供参考: https//developer.android.com/training/basics/fragments/index.html

您可以创建一个基本活动,检查用户的用户名和密码是否已经在SharedPreferences ,如果不存在则启动活动。

例:

public class BeanStalkBaseActivity extends SherlockActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    if(SavedPreference.getUserName(this).length() == 0)
    {
        Intent intent = new Intent(this,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(intent);
    }else
    {
        Intent intent = new Intent(this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(intent);
    }

}

}

BeanStalkBaseActivity应该是你的启动器,因为它只用作检查器。

如果您检查用户是否已经登录主活动或当前活动,然后在登录时切换到另一个活动,这将导致UI故障,即您当前的活动将显示一秒或一半,然后它将切换到目标活动。

你可以这样做:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAuth = FirebaseAuth.getInstance();
    if (mAuth.getCurrentUser() != null) {

        Toast.makeText(MainActivity.this, "Already Logged In", 
        Toast.LENGTH_LONG).show();
        Intent intent = new Intent(MainActivity.this, Home.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

    } else {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        BtnSignUp = findViewById(R.id.btnSignUp);
        BtnLogIn = findViewById(R.id.btnLogIn);


        BtnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent signUp = new Intent(MainActivity.this, SignUpActivity.class);
                startActivity(signUp);

            }
        });

        BtnLogIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent logIn = new Intent(MainActivity.this, Login.class);
                startActivity(logIn);
            }
        });
    }
}

您还可以在启动屏幕活动期间检查登录状态(如果有)。 启动画面非常适合让用户知道应用程序在加载时没有停止,也可以用来将应用程序重定向到适当的屏幕。

我第一次参加这个伟大的指南: https//www.bignerdranch.com/blog/splash-screens-the-right-way/

在主要活动中,只检查用户是否为空,然后启动回家

firebaseAuth = FirebaseAuth.getInstance();

FirebaseUser user = firebaseAuth.getCurrentUser();

if (user != null) {
    finish();
    startActivity(new Intent(MainActivity.this, UserHomeActivity.class));
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM