0

How do I put an if else statement if a button is clicked on my onStart() to check if button clicked is register or log in?

Here is my code

public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register:
                i = new Intent(this, RegisterCustomer.class);
                startActivity(i);
                finish();
                break;
            case R.id.login:
                final String email = memail.getText().toString();
                final String password = mpassword.getText().toString();
                mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivty.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()) {
                            Toast.makeText(CustomerLoginActivty.this, "sign in error", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(firebaseAuthListener);
    }
Rob
  • 2,243
  • 4
  • 29
  • 40
osoda
  • 41
  • 2
  • 6
  • what you want to achieve?? – Akshay Katariya Oct 11 '17 at 14:19
  • To try and put an if else on my onStart() to check what button is being clicked. is it possible? – osoda Oct 11 '17 at 14:20
  • 1
    `onStart()` would have already been called. Can you not put `mAuth.addAuthStateListener(firebaseAuthListener);` in your `onClick()`? – codeMagic Oct 11 '17 at 14:21
  • 1
    Refer this i think you are still unclear with LifeCycle https://stackoverflow.com/a/6812066/4762767 – Akshay Katariya Oct 11 '17 at 14:21
  • why do you want to check the click in onStart? – Nabin Bhandari Oct 11 '17 at 14:22
  • i guess you can not check in onstart as the callbacks will be in onClick of view so it does not matter where you place the logic the response will be in onClick(). – vikas kumar Oct 11 '17 at 14:23
  • or you can check with a flag if onstart is called on onClick – vikas kumar Oct 11 '17 at 14:24
  • If you want to check if button is clicked at once if user stops the app and restarts it, you can also use isClicked =true onRestart() method too. This you can check if button was previously clicked or save flag onsaveinstancestate() and check this onCreate(). It depends on what you wish to achive. – Thracian Oct 11 '17 at 14:48

1 Answers1

0
  1. Create a field in class. e.g. int clickedButton;
  2. In onStart() check the id using that field.

    int clickedButton;
    
    public void onClick(View v) {
        clickedButton = v.getId();
        switch (v.getId()) {
        case R.id.register:
            i = new Intent(this, RegisterCustomer.class);
            startActivity(i);
            finish();
            break;
        case R.id.login:
            final String email = memail.getText().toString();
            final String password = mpassword.getText().toString();
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivty.this, new OnCompleteListener < AuthResult > () {@Override
                public void onComplete(@NonNull Task < AuthResult > task) {
                    if (!task.isSuccessful()) {
                        Toast.makeText(CustomerLoginActivty.this, "sign in error", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            break;
        }
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        if(clickedButton != null){
           switch (clickedButton) {
             case R.id.register:
            //do your thing
             case R.id.login:
            //do your thing
           }
        }
    
        mAuth.addAuthStateListener(firebaseAuthListener);
    
    }
    
Akshar Patel
  • 8,998
  • 6
  • 35
  • 50