How to send a die to an already created Activity?

Asked

Viewed 151 times

2

I’m in the Activity A and I sent you a note from putExtras() to the Activity B that has not yet been created, in this case it worked, but now I have to send a data from Activity B to the Activity A that has already been created and I cannot give finish() in it.

Someone knows how to do?

I’m passing and receiving data through bundle(), but if there’s another way, I’ll apply it too.

  • Activity B will not leave the screen?

  • yes a B is a form after I fill it I saved, I give a Finish() and send a data (just to confirm that it was saved) back to Activity A, this should always be open.

  • 1

    You can create a method in Activitya and call the method created by Activityb.

1 answer

3


You can use the method startActivityForResult() which allows a Activitya call a Activityb and receive a return from it.

To do this, in your Activitya call on the Activityb using the method startActivityForResult(). An example:

Intent i = new Intent(this, ActivityB.class);
/* 
   Crie seu Bundle e coloque dados
*/
startActivityForResult(i, 1); // O '1' é um id para a operação

In his Activityb do the normal procedure of taking the data from Activityaand when you return them write the following code on Activityb:

Intent intentRetorno = new Intent();
intentRetorno.putExtra("resultado", dadoDeRetorno);
setResult(RESULT_OK, intentRetorno);
finish();

Then make your own Activitya implement the method onActivityResult().

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result = data.getStringExtra("resultado");
        }
    }
}
  • Valew man, exactly what I need. It worked right here! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.