Search code examples
androidbuttonandroid-activity

Change text in multi action buttons in Android


I have an Android application where if the user creates an account, they click on the "create an account" button, it takes them to a second activity (DriverDetailsActivity) to fill out a form. Once the form is completed, the user will then click on the "UPDATE" button which will return them to the LoginActivity and the "Create an Account" text should change to "Register" but it is not working.

The code I am supplying isn't working.

enter image description here

LoginActivity:

final Button mRegisterButton = (Button)findViewById(R.id.email_registration_button);
    mRegisterButton.setTag(1);
    mRegisterButton.setText("Create an Account");
    mRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            final int status = (Integer) v.getTag();
            if (status == 1) {
                Intent intent = new Intent(getBaseContext(), DriverDetailsActivity.class);
                startActivity(intent);
                mRegisterButton.setText("Register");
            }
        }
    });

DriverDetailsActivity (2nd Activity)

        // Once completed all fields, user is sent back to DriverLoginActivity
    update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(DriverDetailsActivity.this, DriverLoginActivity.class);
            startActivity(intent);
        }
    });

How can I do this?


Solution

  • Instead of startActivity we have startActivityForResult(intent,requestCode)

    This method is specially used when you want to go to an activity and get some result back.

    use like this :

    In activity A

    startActivityForResult(intent,100);
    

    In activity B when you complete your task what you do is

    setResult(RESULT_OK);
    finish();
    

    In activity A you override this method:

        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            //Do your work here
        }
    }
    

    Apparently you can setResult(RESULT_CANCELLED) also to notify that the result is not correct.

    Hope this helps.