Reputation: 1649
sorry about the cryptic question i am not sure how to word it.
Intent myIntent = new Intent(CurrentClass.this, ClassToCall.class);
myIntent.putExtra("name", "value");
Current.this.startActivity(myIntent);
i know the above code can be used to pass a message from CurrentClass to ClassToCall but when i call the "finish()" method in ClassToCall i want to pass a String back to CurrentClass. can i get some help on how to do this ? once again sorry about how i've worded it.
Upvotes: 0
Views: 199
Reputation: 1300
Create an explicit intent in the ClassToCall like this:
Intent intent = new Intent(ClassToCall.this,CurrentClass.class);
intent.putExtra("Value", "Robert");
startActivity(intent);
finish();
In the CurrentClass:
Bundle extra = getIntent().getExtras();
if(extra != null){
String extras = extra.getString("Value");
Log.e("TAG", " "+extras );
}
Hope this is what you are looking for.
Upvotes: 0
Reputation:
Sometime we need to pass data or parameter to another Activity on Android. Only one activity is active at once. An activity open new activity for result and opened activity need parameter to set their interface or another option based on request. So it is important a system can handle sending and retrieve parameter between two Activity.
Note: The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.
It is really easy to use. You start and activity with a request code and you end it with a result code. The activity caller can then make use of the request code and the result code to implement a switcher and implement some logic. It is clear if you look at the following sample:
startActivityForResult(intent, CREATE_REQUEST_CODE);
In the same activity you need to implement the receiving method. It looks like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CREATE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//ACT
}
}
}
Last part is in the activity being called. On the finish you have to specify the result code:
setResult(RESULT_CANCELED, null);
finish();
//or
setResult(RESULT_OK, null);
finish();
To conclude I advise you to reuse the result codes already implemented in android sdk. In particular you can reuse at least
RESULT_CANCELED : Standard activity result : operation canceled.
RESULT_FIRST_USER : Start of user-defined activity results.
RESULT_OK : Standard activity result : operation succeeded.
Documentation from the developer.android.com
Watch More information about this problem and examples:
Link 1, Link 2 and may be best example(more).
Upvotes: 4