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

android

Solution

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

}

Problem

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

Original source

Related problems