How To Set Edittext Values After Intent Back To Old Activity?
Solution 1:
You can pass data between activities in Intents. Here's how you might structure the code in your case:
Create a static variable in your ActivityA class, which will be used as request code:
publicclassActivityAextendsActivity { //declare a static variable here, in your classpublicstaticfinalintACTIVITYB_REQUEST=100;
Activity A: when button is clicked, create an Intent and startActivityForResult()
Intent intent = newIntent(this, ActivityB.class); startActivityForResult(intent, ACTIVITY_REQUEST);
In Activity B: when clicked an item, store your string in an intent and call setResult() and finish(), which will take you back to Activity A:
//create an IntentIntentresultIntent=newIntent(); //add your string from the clicked item resultIntent.putExtra("string_key", clicked_item_string); //return data back to parent, which is Activity A setResult(RESULT_OK, resultIntent); //finish current activity finish();
In Activity A: override onActivityResult(), check for returned data and set your EditText accordingly
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //check if we are back from Activity B...if (requestCode == ACTIVITYB_REQUEST) { //and all went fine...if (resultCode == RESULT_OK) { //if Intent is not nullif (data != null) { //get your string StringnewString= data.getExtras().getString("string_key"); //set your EditText someEditText.setText(newString); } } } }
Solution 2:
Yes it is possible. You need to simple put the values in the bundle and send that bundle to Activity A through intent.
Intent intent = newIntent(ActivitA.this, ActivtyB.class);
intent.putExtra(name, value);
startActiivty(intent);
Solution 3:
What you are trying to acommplish has 2 parts: Passing the intent value itself to the other activity (most answers already explained it) And second, how to modify the EditText or TextView itself, because if you try to do it inside an OnActivityResultMethod, it wont work directly, and will have to do this workaround:
You can force a EditText.SetText("blablabla..."); inside your OnActivity Result in 3 EASY steps:
- Reload your layout into your Activity
- Rebind your EditText
- use SetText as usual.
In this sample code, I pass a URL string with and intent and write it into a TextView:
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
QRdata= data.getStringExtra("QRURL");
if (QRdata.length()>0)
{
//Step1
setContentView(R.layout.activity_confirmpackage);
//Step2
TextView qrtxt=(TextView)this.findViewById(R.id.qrurl);
//Setp 3,Voilà!
qrtxt.setText(QRdata.toString());
}
Post a Comment for "How To Set Edittext Values After Intent Back To Old Activity?"