Using Intent Between Activities, In Reverse - Android
I am quite new to android programming (aswell as java in general) and have run into an issue that has me stumped. I am building a basic app (for practice) that accesses an already
Solution 1:
Source: Getting a Result from an Activity
From Activity1:
publicstaticfinalStringRESULT_REQUEST=1;
When you want to start Activity2:
Intent intent = newIntent(this, Activity2.class);
startActivityForResult(intent, RESULT_REQUEST);
This will be called when Activity2 is finished:
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding toif (requestCode == RESULT_REQUEST) {
// Make sure the request was successfulif (resultCode == RESULT_OK) {
Stringresult= data.getStringExtra("result");
}
}
}
And Activity2, when you are done and want to send the string back:
IntentreturnIntent=newIntent();
returnIntent.putExtra("result","some string");
setResult(RESULT_OK,returnIntent);
finish();
We have used the RESULT_REQUEST to uniquely identify our own REQUEST, and you do need to check if the requestCode is the same with the one you have started the second activity.
Solution 2:
Put the value in the intent
Intent i = newIntent(SecondScreen.this, FirstScreen.class);
i.putExtra("STRING_I_NEED", strName);
Then, to retrieve the value:
Bundleextras= getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
Post a Comment for "Using Intent Between Activities, In Reverse - Android"