5

How to fetch edittext value from one activity into another activity for string?

Reena
  • 152
  • 8
  • Take a variable and make it as public static. Store the value of edittext in that using .getText().toString(). Now you can use it in any activity. – MRX Feb 22 '13 at 07:22
  • I think this may help you: http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents – Ryu8146 Feb 22 '13 at 07:24
  • What have you searched so far on net please show it here – Abhinav Singh Maurya Feb 22 '13 at 07:28

2 Answers2

2

pass the value in intent ... but if that activity do not appear in the same flow Use SharedPreferences, store the value in the original activity , retrieve it in the later activity.. Shared Preference code:

   SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

    pref.edit().putString("NAME", editText.getText().toString()).commit();

in other activity

    SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
    String ediTextVal = pref.getString("NAME", "anyDefaultValue");

Intent Solution if both activities are in a flow, IN activity1 with the editText :

    Intent intent = new Intent(Activity1.this, Activity2.class);
    intent.putExtra("editTextVal", editText.getText().toString); 
    startActivity(intent);

In activity2 where editText value is needed:

    Bundle extras = getIntent().getExtras(); 
String editTextVal= null;
if(extras !=null && extras.containsKey("editTextVal"))
{
    editTextVal= extras.getString("editTextVal");

}
baboo
  • 1,983
  • 17
  • 23
  • can you help with me a code – Reena Feb 22 '13 at 07:24
  • check my EDIT, if the two activities open in a flow , like the second one opens straight away after the first( in first is edit Text and in 2nd u need value ) then use intent solution , if they are not in flow use sharedpreferences as above – baboo Feb 22 '13 at 07:35
0

You can do this by this way

From FirstActivity

   Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
   intent.putExtra("str", content1); //content1 is String you want to pass in another activity
   startActivity(intent);

From Second Activity to get Data

Intent intent = getIntent();
String str = intent.getStringExtra("str");

Hope this is helpful for you

Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150