0

There are two activities. I want to take the entered in the EditText field dat from the first screen into the second one. I used this code in the first activity:

 Intent i = new Intent(this.getApplicationContext(),MainActivity.class);
   i.putExtra((inputuserName.getText()).toString(), true);
   startActivity(i);

How can access the value in the second one? Thanks!

user6456773
  • 381
  • 2
  • 10
  • 18
  • Possible duplicate of [Pass a String from one Activity to another Activity in Android](http://stackoverflow.com/questions/6707900/pass-a-string-from-one-activity-to-another-activity-in-android) – Moulesh Sep 18 '16 at 14:46
  • Possible duplicate of [Passing data between activities in Android](http://stackoverflow.com/questions/2965109/passing-data-between-activities-in-android) – K Neeraj Lal Sep 18 '16 at 14:46

2 Answers2

2

hi why you trying to send boolean type true or false ? You need to send only String value. See,

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
i.putExtra("STRING_I_NEED", inputuserName.getText()).toString());
startActivity(i);

Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

Thanks

Saveen
  • 4,120
  • 14
  • 38
  • 41
  • Thank you! But the problem is that I am not passing a simple string, but value, entered in Screen A to Screen B. How can I access it in Screen B? – user6456773 Sep 18 '16 at 14:51
  • i'm getting your edittext value send to next activity, just call second step in your oncreate in SecondScreen – Saveen Sep 18 '16 at 14:55
  • i have send value **STRING_I_NEED** and get it in SecondScreen. Please check – Saveen Sep 18 '16 at 14:57
  • Yes, on this way it works perfect, as you give a static String. What I need is to take the data from an editText, entered by the user, and to pass it as a value within the second class. I have the getText() method. However, I cannot access the EditText within the SecondActivity. – user6456773 Sep 18 '16 at 14:59
  • No you dont need anything static edittext. You already have same value in your second screen. So, where you want to use you can use. – Saveen Sep 18 '16 at 15:03
  • Did you fix your problem ? – Saveen Sep 18 '16 at 16:52
  • Yes! Just was typing the answer! Thank you very much, everything is perfect now :) – user6456773 Sep 18 '16 at 16:57
0

You have to attach a message to the value you're trying to pass: i.putExtra("STRING_I_NEED", strName); So you want to put inputuserName.getText()).toString() where strName is.

In Next activity: Bundle extras = getIntent().getExtras(); newString= extras.getString("STRING_I_NEED");