-2

I would like to pass data from an activity to an other using intent but it won't work. I think I did something wrong in my code.

        if (condition) {
            Hero = arrayP.get(0).name;
            Intent i = new Intent(Activity2.this, Activity3.class);
            i.putExtra("name");
            startActivity(i);

        }
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    Try this https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – DawidJ Dec 28 '18 at 13:46

2 Answers2

0

@Fra,

You code is bit incorrect. You can pass data from one activity to another activity by using

Intent i = new Intent(Activity2.this, Activity3.class);
i.putExtra("Key", "Value");
startActivity(i);

And inside Activity3, you can get data using

String data = getIntent().getStringExtra("Key");

Also as this is very simple part, you should research it on google before asking in Stackoverflow.

Ajay Mehta
  • 843
  • 5
  • 14
0

look for it to work properly you should make the following changes.

you must change this:

        if (condition) {
            Hero = arrayP.get(0).name;
            Intent i = new Intent(Activity2.this, Activity3.class);
            i.putExtra("name");
            startActivity(i);

        }

to:

            if (condition) {
                String heroName = arrayP.get(0).name; // assuming that name is a String and that arrayP is a Hero Array
                Intent i = new Intent(Activity2.this, Activity3.class);
                i.putExtra("name", heroName); // change
                startActivity(i);

            }
Juanes30
  • 2,398
  • 2
  • 24
  • 38