I start a new Activity
from the original Activity
with startActivityForResult(intent, requestCode)
. I want to return data to the original Activity
when the user presses the back button, so when returns to the original Activity
. I tried two methods:
overriding onBackPressed()
:
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.putIntegerArrayListExtra(SELECTION_LIST, selected);
setResult(RESULT_OK, intent);
}
overriding onPause()
:
@Override
protected void onPause() {
super.onPause();
Intent intent = new Intent();
intent.putIntegerArrayListExtra(SELECTION_LIST, selected);
setResult(RESULT_OK, intent);
}
Unfortunately, none of them worked (resultCode
is not RESULT_OK
in onActivityResult()
). What's the proper way to do this? Thanks!
You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.
An affinity indicates which task an activity prefers to belong to. By default, all the activities from the same app have an affinity for each other. So, by default, all activities in the same app prefer to be in the same task. However, you can modify the default affinity for an activity.
FLAG_ACTIVITY_NEW_TASK is equivalent to launchMode=singleTask and in there I read. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.
Try this :
@Override
public void onBackPressed() {
// super.onBackPressed();
Intent intent = new Intent();
intent.putIntegerArrayListExtra(SELECTION_LIST, selected);
setResult(RESULT_OK, intent);
finish();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With