我的应用程序直接进入MainActivity

时间:2019-04-21 15:09:20

标签: java android

我正在创建类似于WhatsApp的应用程序,但是我遇到了问题。该应用程序假设可以进入LoginActivity,但无需我登录即可直接进入MainActivity。

我尝试更改android清单中的启动器场景,并且发生了相同的事情。我尝试在两个public class EnemyResult{ private ArrayList<Enemy> results; public ArrayList<Enemy> getResults() { return results; } public void setResults(ArrayList<Enemy> results) { this.results = results; } 的末尾添加mAuth.signOut();。我试图改变这个 loadingBar.dismiss();与此SendUserToMainActivity();。我也删除了Firebase数据库中的所有帐户。

LoginActivity:

SendUserToRegisterActivity();

AndroidManifest:

private FirebaseUser currentUser;
private FirebaseAuth mAuth;
private ProgressDialog loadingBar;

private Button LoginButton, PhoneLoginButton;
private EditText UserEmail, UserPassword;
private TextView NeedNewAccountLink, ForgetPasswordLink;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();

    InitializeFields();

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

            SendUserToRegisterActivity();

        }
    });


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

            AllowUserToLogin();

        }
    });


}

private void AllowUserToLogin() {

    String email = UserEmail.getText().toString();
    String password = UserPassword.getText().toString();

    if(TextUtils.isEmpty(email)){

        Toast.makeText(this, "Please enter your email", Toast.LENGTH_SHORT).show();
    }

    if(TextUtils.isEmpty(password)){

        Toast.makeText(this, "Please enter your password", Toast.LENGTH_SHORT).show();
    }
    else{

        loadingBar.setTitle("Logging In");
        loadingBar.setMessage("Please wait...");
        loadingBar.setCanceledOnTouchOutside(true);
        loadingBar.show();

        mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if(task.isSuccessful())
                        {
                            SendUserToMainActivity();
                            Toast.makeText(LoginActivity.this, "Logged in successfully", Toast.LENGTH_SHORT).show();
                            loadingBar.dismiss();
                        }

                            else{

                                String message = task.getException().toString();
                                Toast.makeText(LoginActivity.this, "Error:" + message, Toast.LENGTH_SHORT).show();
                                loadingBar.dismiss();

                            }
                    }
                });

    }

}

private void InitializeFields() {

    LoginButton = (Button) findViewById(R.id.login_button);
    PhoneLoginButton = (Button) findViewById(R.id.phone_login_button);
    UserEmail = (EditText) findViewById(R.id.login_email);
    UserPassword = (EditText) findViewById(R.id.login_password);
    NeedNewAccountLink = (TextView) findViewById(R.id.need_new_account_link);
    ForgetPasswordLink = (TextView) findViewById(R.id.forget_password_link);
    loadingBar = new ProgressDialog(this);

}

@Override
protected void onStart() {
    super.onStart();

    if (currentUser != null){

        SendUserToMainActivity();

    }
}

private void SendUserToMainActivity() {

        Intent loginIntent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(loginIntent);
    }

private void SendUserToRegisterActivity() {

    Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
    startActivity(registerIntent);
}

3 个答案:

答案 0 :(得分:2)

清除一次应用缓存,将启动器从Main Activity更改为Login Activity重建应用,然后在LoginActivity的onStart上登录,这就是为什么他总是将您发送到mainActivity的原因,
如果您删除了Firebase中的所有用户,那么在清除缓存之前,您将获得身份验证。
尝试此解决方案,让我知道它是否适合您。

答案 1 :(得分:1)

AllowUserToLogin()中,您必须放置

return;
在显示Toast并显示错误消息后,

声明。
还要在检查密码后删除else

if(TextUtils.isEmpty(email)){
    Toast.makeText(this, "Please enter your email", Toast.LENGTH_SHORT).show();
    return;
}

if(TextUtils.isEmpty(password)){
    Toast.makeText(this, "Please enter your password", Toast.LENGTH_SHORT).show();
    return;
}
loadingBar.setTitle("Logging In");
..................................

修改
在您发布清单后,尽管您在问题中提到:

,但我发现您的LAUNCHER活动为MainActivity

  

我尝试更改android清单中的启动器场景   事情正在发生。

您还必须将LAUNCHER活动更改为LoginActivity

答案 2 :(得分:1)

像这样更改清单。

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".RegisterActivity" />
<activity android:name=".LoginActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".MainActivity"/>

打开登录活动时,检查是否已登录的用户将其发送到MainAcitivty。

如果您有一种方法,您将在其中检查文本字段验证,这也很好。 像这样:

public boolean verified() {
  if(TextUtils.isEmpty(email)){
    Toast.makeText(this, "Please enter your email", Toast.LENGTH_SHORT).show();
    return false;
  }

  if(TextUtils.isEmpty(password)){
      Toast.makeText(this, "Please enter your password", 
      Toast.LENGTH_SHORT).show();
   return false;
  }

 return true;
}

并这样做:

if(verified){
    loadingBar.setTitle("Logging In");
    loadingBar.setMessage("Please wait...");
    loadingBar.setCanceledOnTouchOutside(true);
    loadingBar.show();
    //And so on.
}