0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
LizG
  • 2,246
  • 1
  • 23
  • 38

3 Answers3

2

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.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
  • Just to clarify I am putting these in the right areas: startActivityForResult(intent, 100) is replacing startActivity(intent). setResult(...) is before Intent intent ...?? – LizG Jul 11 '17 at 16:19
  • 1
    Yes. The 100 is actually is a request code which can be changed to any value. Then in onActivityResult we match this if it is from the same request code that we sent. – Sarthak Gandhi Jul 11 '17 at 16:21
  • Activity A final Button mRegisterButton = (Button)findViewById(R.id.email_registration_button); mRegisterButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick (View v) { Intent intent = new Intent(DriverLoginActivity.this, DriverDetailsActivity.class); startActivityForResult(intent,100); } }); then outside of onCreate is the Override function – LizG Jul 11 '17 at 16:24
  • 1
    Perfect.!! Happy coding. – Sarthak Gandhi Jul 11 '17 at 16:25
  • Its still not working, I must be doing something wrong – LizG Jul 11 '17 at 16:26
  • Activity B: update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { setResult(RESULT_OK); finish(); Intent intent = new Intent(DriverDetailsActivity.this, DriverLoginActivity.class); intent.putExtra("BACK", "Register"); startActivity(intent); } }); – LizG Jul 11 '17 at 16:27
  • 1
    No don't start the activity again. Remove the Intent intent from the activity b – Sarthak Gandhi Jul 11 '17 at 16:30
  • OK, did that., but with Activity A: if (requestCode == 100 && resultCode == RESULT_OK) { //Do your work here mRegisterButton.setText("Register"); } setting the text seems to crash the app, how do I change the text, so I can implement the registration – LizG Jul 11 '17 at 16:34
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148919/discussion-between-sarthak-gandhi-and-lizg). – Sarthak Gandhi Jul 11 '17 at 16:35
0

For this you can use startActivityForResult() to get result from DriverDetailsActivity and update text in onActivityResult.

For more info check these links

https://developer.android.com/training/basics/intents/result.html

How to use StartActivityForResult()

Dhanumjay
  • 540
  • 7
  • 17
0

Try this

LoginActivity:

if( getIntent().getExtras() != null) {
            btnOne.setText(getIntent().getStringExtra("BACK"));
        }else{
            btnOne.setText("Create");
        }


        btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(btnOne.getText().toString().equalsIgnoreCase("Create")){
                    Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                    startActivity(intent);
                }else{
                    //do registration
                }
            }
        });

DriverDetailsActivity (2nd Activity)

btnTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(SecondActivity.this,MainActivity.class);
                    intent.putExtra("BACK","Register");
                    startActivity(intent);
            }
        });
Arnold Brown
  • 1,330
  • 13
  • 28
  • Arnold Brown This did not work. Keeps giving me an error that application has stopped. I put getIntent .. (LoginActivity) under setContentView in onCreate and it gave error. – LizG Jul 11 '17 at 16:04
  • All fixed. Tks anyway – LizG Jul 12 '17 at 14:15