0

Please am designing an android app, i want to send (5)String values from one activity to another activity to use in different TextViews, i have tried virtually all the code i could find online on the topic, but i keep getting just one value(the last value i send in the putExtra()). please i am new to Android Studio and will appreciate every help.

I have used the putExtra() to send one data to another activity and it worked perfectly, while trying to do the same with multiple data i keep getting just one of the data sent. I have also tried using a bundle object, to receive the data from the other(recieving) activity.

I expect getting all this data ( intent.putExtra("surname", "Jerry").
intent.putExtra("middlename", "chris"). intent.putExtra("lastname", "Enema")) in another activity, but i keep getting just "Enema" alone

this is my code; //in the firstActivity

     send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String sFirstname = firstname.getText().toString();
                String sLastname = lastname.getText().toString();

                Intent intent = new Intent(MainActivity.this, ReceiveActivity.class);

                intent.putExtra("surname" ,sFirstname);
                intent.putExtra("lastname", sLastname);

                startActivity(intent);

            }
        });

//And In the second Activity

firstname = findViewById(R.id.firstname); lastname = findViewById(R.id.firstname);

Intent intent = getIntent();
Bundle bundle = getIntent().getExtras();

String ssurname = bundle.getString("surname");
String slastname = bundle.getString("lastname");

firstname.setText(ssurname);
lastname.setText(slastname);
jchrix
  • 5
  • 1

2 Answers2

0

I would prefer not to ue getExtras so you should have two getExtra Like this :

 Intent intent = getIntent();

 String ssurname = intent.getExtra("surname");
 String slastname = intent.getExtra("lastname");

 firstname.setText(ssurname);
 lastname.setText(slastname);
Iman Nia
  • 2,255
  • 2
  • 15
  • 35
0

Try this. In First Activity:

String sFirstname = "Tope";
String sLastname = "Adebodun";

Intent theIntent = new Intent(MainActivity.this, ReceivingActivity.class);
theIntent.putExtra("firstname", sFirstname);
theIntent.putExtra("lastname", sLastname);

Then in your second activity, do this in the onCreate method:

Intent intent = getIntent();
String thefirst = (String) intent.getExtras.getString("firstname");
String thelast = (String) intent.getExtras.getString("lastname");
  • Thank you soo much, for some reason this fixed my code, i created a new project and tried this code, i am still going over my previous project to find the errors, thank you all so very much, i do appreciate all your help. :) – jchrix Sep 05 '19 at 15:54