0

I have been trying to create this login/registration app in Android Studio, but I am having an issue. When I go to the Registration Activity I cannot get back to the login Activity by clicking on the TextView I set an OnClickListener for. Also, when I press the sign up button on the Registration Activity, it should take me back to the Login/Main Activity and it doesn't. Below I have the code for both Activites plus the manifests. Could someone tell me where my mistake is?

MANIFEST

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.cedricpaige.mercurylogin">

<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">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MainActivity" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecondActivity" />
    <activity android:name=".RegActivity"></activity>
</application>

</manifest>

MAIN ACTIVITY

public class MainActivity extends AppCompatActivity {


private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
private TextView userReg;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;


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

    Name = (EditText) findViewById(R.id.nameText);
    Password = (EditText) findViewById(R.id.passText);
    Info = (TextView) findViewById(R.id.textView);
    userReg = (TextView) findViewById(R.id.tvReg);
    Login = (Button) findViewById(R.id.loginButton);

    Info.setText("No. of attempts remaining: 5");


    firebaseAuth = FirebaseAuth.getInstance();
    progressDialog = new ProgressDialog(this);

    FirebaseUser user = firebaseAuth.getCurrentUser();

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


    Login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            validate(Name.getText().toString(), Password.getText().toString());
        }
    });

    userReg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, RegActivity.class));
        }
    });

}


private void validate(String userName, String userPassword) {

    progressDialog.setMessage("Signing In!");
    progressDialog.show();

    firebaseAuth.signInWithEmailAndPassword(userName, userPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful()) {
                progressDialog.dismiss();
                Toast.makeText(MainActivity.this, "Login Successful!", Toast.LENGTH_SHORT).show();

                startActivity(new Intent(MainActivity.this, SecondActivity.class));

            }else {
                Toast.makeText(MainActivity.this, "Login Failed!", Toast.LENGTH_SHORT).show();
                counter--;
                Info.setText("Number of attempts remaining: " + counter);
                progressDialog.dismiss();
                if (counter == 0) {
                    Login.setEnabled(false);
                }

            }
        }
    });

}

}

REGISTRATION ACTIVITY

public class RegActivity extends AppCompatActivity {

private EditText userName, userPassword, userEmail;
private Button regButton;
private TextView userLogin;
private FirebaseAuth firebaseAuth;


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


    firebaseAuth = FirebaseAuth.getInstance();


    regButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (validate()) {


                String user_email = userEmail.getText().toString().trim();
                String pass_word = userPassword.getText().toString().trim();

                firebaseAuth.createUserWithEmailAndPassword(user_email,pass_word).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if (task.isSuccessful()) {
                        Toast.makeText(RegActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(RegActivity.this, MainActivity.class));
                    }else {
                            Toast.makeText(RegActivity.this, "Registration Failed!", Toast.LENGTH_SHORT).show();

                        }
                        }
                });

            }
        }
    });


    userLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(RegActivity.this, MainActivity.class));
        }
    });

}
    private void setupUIViews() {

    userName = (EditText)findViewById(R.id.etUserName);
    userPassword = (EditText)findViewById(R.id.etPassword);
    userEmail = (EditText)findViewById(R.id.etEmail);
    regButton = (Button)findViewById(R.id.buttonSignUp);
    userLogin = (TextView)findViewById(R.id.textviewAlready);

    }

    private Boolean validate() {
        Boolean result = false;

        String name = userName.getText().toString();
        String password = userPassword.getText().toString();
        String email = userEmail.getText().toString();

        if (name.isEmpty() || password.isEmpty() || email.isEmpty()) {

            Toast.makeText(this,"Please enter all details", Toast.LENGTH_SHORT).show();

        }else {
            result = true;
        }

        return result;

    }

1 Answers1

0

In your MainActivity, you have this line of code in your onCreate()

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

This is just working like an authListener(), when your Activity starts it will see if the user is != null to enter in that block.

When you use this code in your RegisterActivity

 regButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (validate()) {


                String user_email = userEmail.getText().toString().trim();
                String pass_word = userPassword.getText().toString().trim();

                firebaseAuth.createUserWithEmailAndPassword(user_email,pass_word).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if (task.isSuccessful()) {
                        Toast.makeText(RegActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(RegActivity.this, MainActivity.class));
                    }else {
                            Toast.makeText(RegActivity.this, "Registration Failed!", Toast.LENGTH_SHORT).show();

                        }
                        }
                });

            }
        }
    });

after if (task.isSuccessful()) {... you are starting again the MainActivity, which will go to check again inside onCreate() if the user is != null.

Since you just registered an user, automatically getCurrentUser() != null , so it will enter in that if statment and finish your MainActivity.class , and then relaunch your RegisterActivity.class

A simple solution will be instead of bringing the user back to the login activity (MainActivity.class), send your user to the Activity after they log in, since now the user is !=null you don't need to request for a login again to the user.

If that is not your use case, just by removing finish() from your MainActivity will do the job, and instead of doing another intent at your RegisterActivity, just do a finish after task.isSuccefull

You can place a debug breakpoint at that if statement in your MainActivity to see the changes of the user, and then you can be sure that the user is null or not

Tip

I see you use uppercase names for your variables, instead of doing this

private Button Login;

do this

private Button mBtnLogin;

It will not change anything at your code, but defining your variables with CamelCase will decrease the risk in the future of your app development to confuse it with some method call

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77