How do I get ActivitB Intent values and move to ActivityA in a List?

1

I have a ActivityA that is the main, it contains a ListView and a botão , and I have a ActivityB that contains two EditText and a button.

When start app starts in ActivityA with empty list, pressing the button executes the following code

 Intent it = new Intent(this, ActivityB.class);
 startActivity(it); 

Now I am in ActivityB I fill in the EditTex and press the button that executes the following code

   String texto = editTexto.getText().toString();
   double numero = Double.parseDouble(editnumero.getText().toString());


        Intent it = new Intent(this, ActivityA.class);
        it.putExtra("texto", texto);
        it.putExtra("numero", numero);
        startActivity(it);

So I'm in ActivityA , inside onCreate follows code

     Intent it  = getIntent();
     texto = it.getStringExtra("texto");
     numero = it.getDoubleExtra("numero", -1);

        if(texto != null)
     //List<Carro> carro = new ArrayLis<Carro>(); variável global
        carro.add( new Desejo(texto,numero));
        carroAdapter  = new ArrayAdapter<Carro>(this, android.R.layout.simple_list_item_1,carro );
        listaCarro.setAdapter(carroAdapter);
    }

Ok, it works, but when I click the button again I go to ActivityB , I type the values and click the button, I go to ActivityA .

    
asked by anonymous 14.04.2017 / 16:17

1 answer

3

When an Activity is brought to foreground one of 3 methods is called and receives intent

  • onCreate() - When a new instance of Activity is created.
  • onNewIntent() - When there is an instance of Activity and AndroidManifest.xml the launchMode attribute was declared as "singleTop" or the intent contains the flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP "set".
  • onActivityResult() - When activity launches another, through startActivityForResult() , and this is terminated.

In order for ActivityA to maintain its data and to add data from ActivityB, you must ensure that intent used in ActivityB, to call ActivityA, does not create another instance of ActivityA. Home To do this, use the activity declaration in AndroidManifest.xml, the

android:launchMode="singleTop"

or create intent this way

Intent it = new Intent(this, ActivityA.class);
it.putExtra("texto", texto);
it.putExtra("numero", numero);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(it);

Whatever form is adopted, when the ActivityA passes to foreground , the method that is called is onNewIntent() . So you should pass the code that updates the ListView from onCreate() to it.

@Override
protected void onNewIntent(Intent intent) {

    String texto = intent.getStringExtra("texto");
    String numero = intent.getDoubleExtra("numero", -1);

    if(texto != null)
    carro.add( new Desejo(texto,numero));
    carroAdapter  = new ArrayAdapter<Carro>(this, android.R.layout.simple_list_item_1,carro );
    listaCarro.setAdapter(carroAdapter);
}

Normally the approach used in cases like this - use a second activity to get data for the first - is to launch the second activity with onActivityResult ()
For an example see How to return data from the Activity call to the one that called it? .

    
14.04.2017 / 17:16