How to take Activitb’s Intent values and move to Activitya in a List?

Asked

Viewed 1,792 times

1

I have a ActivityA which is the main one, contains a ListView and a botão, and I have a ActivityB containing two EditText and a button.

When do I start app begins in the ActivityA with empty list, when pressing the button executes the following code

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

Now I’m in the ActivityB 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 am I in the ActivityA, within the onCreate follows the 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);
    }

Okay, it works, but when I click the button again I go to ActivityB, I type the values and I click the button, I go to ActivityA. what had been added earlier was gone and the last data was triggered in the list.

1 answer

3


When an Activity is brought to foreground one of 3 methods is called and receives the Intent that gave rise to that fact:

  • onCreate() - When a new Activity instance is created.
  • onNewIntent() - When there is an instance of Activity and in Androidmanifest.xml the attribute launchMode was declared as "singleTop" or the flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP "arrows".
  • onActivityResult()- When the Activity another spear, through startActivityForResult(), and this is finished.

In order for Activitya to keep your data and to add data from Activityb, you must ensure that the Intent used in Activityb, to call Activitya, do not create another instance of Activitya.
For this use, in the Activity statement on Androidmanifest.xml, the attribute

android:launchMode="singleTop"

or create the Intent in 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 Activitya passes to foreground, the method that is called is onNewIntent().
Thus, you must pass the code that updates the onCreate() for him.

@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 - using a second Activity to get data for the first - is to launch the second Activity with startActivityForResult() and receive the data (input) in the method onActivityResult().
For an example see How to return data from Call Activity to the one who called it?.

Browser other questions tagged

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