Search code examples
androidextras

All extras Strings take the value of the last one put into the bundle


I am working on an android app where the user fills in one activity, then another one and a word docx report is generated.

I am using extras to pass Strings from the first activity to the second one.

My problem is that all my Strings have the value of the last String put into extras. Here is my code :

Activity 1 :

Intent pelle = new Intent(VGP1.this, Pelle.class);
Bundle extras = new Bundle();
extras.putString(eEntreprise, sEntrepriseComplete);
extras.putString(eAdresse, sAdresseComplete);
extras.putString(eIdVerificateur, editTextIdVerificateur.getText().toString());
extras.putString(eNomProprio, editTextNomProprio.getText().toString());
extras.putString(eAdresseProprio, editTextAdresseProprio.getText().toString());
extras.putString(eNomEntreprise, editTextNomEntreprise.getText().toString());
extras.putString(eAdresseEntreprise, editTextAdresseEntreprise.getText().toString());
extras.putString(eJourVerif, sJourVerif);
extras.putString(eMoisVerif, sMoisVerif);
extras.putString(eAnneeVerif, sAnneeVerif);
pelle.putExtras(extras);
startActivity(pelle);

Activity 2 :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pelle);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        eEntreprise2 = extras.getString(VGP1.eEntreprise);
        eAdresse2 = extras.getString(VGP1.eAdresse);
        eIdVerificateur2 = extras.getString(VGP1.eIdVerificateur);
        eNomProprio2 = extras.getString(VGP1.eNomProprio);
        eAdresseProprio2 = extras.getString(VGP1.eAdresseProprio);
        eNomEntreprise2 = extras.getString(VGP1.eNomEntreprise);
        eAdresseEntreprise2 = extras.getString(VGP1.eAdresseEntreprise);
        eJourVerif2 = extras.getString(VGP1.eJourVerif);
        eMoisVerif2 = extras.getString(VGP1.eMoisVerif);
        eAnneeVerif2 = extras.getString(VGP1.eAnneeVerif);
    }

All the values "xxxxx2" take the value of eAnneeVerif, I don't understand why.

Thank you in advance.


Solution

  • Try this code:

    In your Activity 1 send data like this:

    Intent pelle = new Intent(VGP1.this, Pelle.class);
    pelle.putExtra("eEntreprise", sEntrepriseComplete);
    pelle.putExtra("eAdresse", sAdresseComplete);
    pelle.putExtra("eIdVerificateur", editTextIdVerificateur.getText().toString());
    pelle.putExtra("eNomProprio", editTextNomProprio.getText().toString());
    pelle.putExtra("eAdresseProprio", editTextAdresseProprio.getText().toString());
    pelle.putExtra("eNomEntreprise", editTextNomEntreprise.getText().toString());
    pelle.putExtra("eAdresseEntreprise", editTextAdresseEntreprise.getText().toString());
    pelle.putExtra("eJourVerif", sJourVerif);
    pelle.putExtra("eMoisVerif", sMoisVerif);
    pelle.putExtra("eAnneeVerif", sAnneeVerif);
    startActivity(pelle);
    

    In your Activity 2 get data like this:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pelle);
    
        Intent intent = getIntent();
        eEntreprise2 = intent.getStringExtra("eEntreprise");
        eAdresse2 = intent.getStringExtra("eAdresse");
        eIdVerificateur2 = intent.getStringExtra("eIdVerificateur");
        eNomProprio2 = intent.getStringExtra("eNomProprio");
        eAdresseProprio2 = intent.getStringExtra("eAdresseProprio");
        eNomEntreprise2 = intent.getStringExtra("eNomEntreprise");
        eAdresseEntreprise2 = intent.getStringExtra("eAdresseEntreprise");
        eJourVerif2 = intent.getStringExtra("eJourVerif");
        eMoisVerif2 = intent.getStringExtra("eMoisVerif");
        eAnneeVerif2 = intent.getStringExtra("eAnneeVerif");
    
    }