setResult does not work when BACK button pressed

我正在设置返回按钮按下后的结果

Intent data = new Intent();
setResult(RESULT_OK, data)

但是一旦涉及到

onActivityResult(int requestCode, int resultCode, Intent data)

the resultCode is 0 (RESULT_CANCELED) and data is 'null'.

那么,如何传递由 BACK 按钮终止的活动的结果呢?

61831 次浏览

onDestroy is too late in the chain — instead override onPause and check isFinishing() to check if your activity is at the end of its lifecycle.

我重构了我的代码。最初,我准备了一些数据,并将其设置为 onDestroy中的 activity result(这不起作用)。现在,每次更新要返回的数据时,我都设置 activity数据,而 onDestroy中没有任何数据。

Activity result must be set before finish() is called. Clicking BACK actually calls finish() on your activity, so you can use the following snippet:

@Override
public void finish() {
Intent data = new Intent();
setResult(RESULT_OK, data);


super.finish();
}

如果在 onOptionsItemSelected()中调用 NavUtils.navigateUpFromSameTask();,就会调用 finish(),但是会得到错误的 result code。所以你必须在 onOptionsItemSelected()中调用 finish()而不是 navigateUpFromSameTaskOnActivityResult 中错误的 request 代码

请参阅 OnActivityResult (int,int,Inent) doc

解决方案是检查 Result 代码的值 返回文章页面活动:。如果是,则表示已按下 BACK 或活动已崩溃。希望对你们有用,对我也有用。

尝试重写 onBackPress (从 android 级别5开始) ,或者重写 onKeyDown ()并捕获 KeyEvent.BUTTON _ Back (参见 Android 活动结果) 这对我有用。

您需要重写 onBackPressed()方法并将结果 之前设置为对超类的调用,即

@Override
public void onBackPressed() {
Bundle bundle = new Bundle();
bundle.putString(FIELD_A, mA.getText().toString());
    

Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
super.onBackPressed();
}

Don't rely on any logic executed in onPause of one activity when you return to the initial one. According to the docs:

此方法(onPuse)的实现必须非常快,因为 在此方法返回之前,将不会继续下一个活动

详情请参阅 http://goo.gl/8S2Y

最安全的方法是在每次结果修改操作完成后设置结果(正如您在回答中提到的)

您应该像下面这样覆盖 OnOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
final Intent mIntent = new Intent();
mIntent.putExtra("param", "value");
setResult(RESULT_OK, mIntent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

我粘贴的答案可能会对其他人有帮助: 当用 android 设置 launcheMode 时: launchMode = “ singleTask” 我也得不到结果, 医生说:

     /* <p>Note that this method should only be used with Intent protocols
* that are defined to return a result.  In other protocols (such as
* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
* not get the result when you expect.  For example, if the activity you
* are launching uses the singleTask launch mode, it will not run in your
* task and thus you will immediately receive a cancel result.
*/

and:

     /* <p>As a special case, if you call startActivityForResult() with a requestCode
* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
* activity, then your window will not be displayed until a result is
* returned back from the started activity.  This is to avoid visible
* flickering when redirecting to another activity.
*/

如果您想在 onBackPressed事件中设置一些自定义 RESULT_CODE,那么您需要首先设置 result,然后调用 super.onBackPressed(),您将在调用者活动的 onActivityResult方法中收到相同的 RESULT_CODE

    @Override
public void onBackPressed()
{
setResult(SOME_INTEGER);
super.onBackPressed();
}