How to send a data to an already created activity?

2

I am in activity A and I sent a data by putExtras() to activity B that has not yet been created, in this case it worked, but now I have to send a given from activity B to activity A that has already been created and I can not give finish() to it.

Does anyone know how to do it?

I am passing and receiving data by bundle() , but if I have another form I also apply.

    
asked by anonymous 07.08.2015 / 22:23

1 answer

3

You can use the startActivityForResult() method that allows an to call an ActivityB and receive a return from it.

To do this, in your ActivityA call ActivityB using the startActivityForResult() method. 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 your ActivityB do the normal procedure to get ActivityA data and when you return them, type the following code in ActivityB :

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

Then, make your ActivityA implement the onActivityResult() method.

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

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result = data.getStringExtra("resultado");
        }
    }
}
    
07.08.2015 / 22:50