简体   繁体   中英

How do I use One login screen activity for different activities in Android?

i'm currently developing an Android app in Android Studio and i'm trying to use only one login screen for two different screens instead of using two login screens which will use the phone memory more. I tried using if and else statements where if button 1 was clicked then send to screen 1. Below is my code:

public void userLogin() {

    String userEmail = email.getText().toString();
    String userPass = password.getText().toString();

    if(TextUtils.isEmpty(userEmail)){
        Toast.makeText(LoginActivity.this, "Please Enter Email", Toast.LENGTH_SHORT).show();
    }

    if(TextUtils.isEmpty(userPass)){
        Toast.makeText(LoginActivity.this, "Please Enter Password", Toast.LENGTH_SHORT).show();
    }


    auth.signInWithEmailAndPassword(userEmail, userPass)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        progressDialog.dismiss();
                        finish();
                        if (mainactivity.type == 1) {
                            Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(LoginActivity.this, EmployeeActivity.class));
                        }
                        if(mainactivity.type == 2){
                            Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                        }
                    }else{
                        Toast.makeText(LoginActivity.this, "ERROR: Invalid Email or Password", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }
                    }

                });
            }

Crash Error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.javytharanee.quicksolattendance, PID: 7078
              android.content.ActivityNotFoundException: No Activity found to handle Intent {  }
                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2007)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
                  at android.app.Activity.startActivityForResult(Activity.java:4586)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
                  at android.app.Activity.startActivityForResult(Activity.java:4544)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
                  at android.app.Activity.startActivity(Activity.java:4905)
                  at android.app.Activity.startActivity(Activity.java:4873)
                  at com.javytharanee.quicksolattendance.LoginActivity$2$override.onComplete(LoginActivity.java:89)
                  at com.javytharanee.quicksolattendance.LoginActivity$2$override.access$dispatch(Unknown Source:86)
                  at com.javytharanee.quicksolattendance.LoginActivity$2.onComplete(LoginActivity.java:77)
                  at com.google.android.gms.tasks.zzf.run(Unknown Source:23)
                  at android.os.Handler.handleCallback(Handler.java:873)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:193)
                  at android.app.ActivityThread.main(ActivityThread.java:6669)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Any help would be much appreciated as i'm totally new to app development.

Try calling finish() after startActivity() . You are losing context to your activity hence LoginActivity.this is return null.

Like this

if(mainactivity.type == 1){
   Toast.makeToast(LoginActivity.this, "Message", Toast.LENGTH_SHORT);
   startActivity(new Intent(LoginActivity.this, EmployeeActivity.class));
}

if(mainactivity.type == 2){
   Toast.makeToast(LoginActivity.this, "Message", Toast.LENGTH_SHORT);
   startActivity(new Intent(LoginActivity.this, 
   Employee2Activity.class));
}

LoginActivity.this.finish();

public void userLogin() {

    String userEmail = email.getText().toString();
    String userPass = password.getText().toString();
    Log.e(TAG, "userLogin: "+userEmail  +userPass );
    if (TextUtils.isEmpty(userEmail)) {
        Toast.makeText(LoginActivity.this, "Please Enter Email", Toast.LENGTH_SHORT).show();
    } else if (TextUtils.isEmpty(userPass)) {
        Toast.makeText(LoginActivity.this, "Please Enter Password", Toast.LENGTH_SHORT).show();
    } else {
        Log.e(TAG, "userLogin: " );
        auth.signInWithEmailAndPassword(userEmail, userPass)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            progressDialog.dismiss();
                            Log.e(TAG, "onComplete: "+mainactivity.type );
                            if (mainactivity.type == 1) {
                                Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(LoginActivity.this, EmployeeActivity.class));
                                finish();
                            } else if (mainactivity.type == 2) {
                                Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                                finish();
                            }
                        } else {
                            Toast.makeText(LoginActivity.this, "ERROR: Invalid Email or Password", Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                        }
                    }

                });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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