Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch edit text value for as a string value in one activity from another activity? [duplicate]

Tags:

android

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

like image 331
Reena Avatar asked Feb 22 '13 07:02

Reena


1 Answers

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");

}
like image 167
baboo Avatar answered Nov 14 '22 23:11

baboo