Android 按下后退按钮时 setResult 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2679250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setResult does not work when BACK button pressed
提问by alex2k8
I am trying to setResult after the BACK button was pressed. I call in onDestroy
我试图在按下 BACK 按钮后设置结果。我调用 onDestroy
Intent data = new Intent();
setResult(RESULT_OK, data)
But when it comes to
但是当谈到
onActivityResult(int requestCode, int resultCode, Intent data)
the resultCode is 0 (RESULT_CANCELED) and data is 'null'.
结果代码为 0 (RESULT_CANCELED),数据为“空”。
So, how can I pass result from activity terminated by BACK button?
那么,如何传递由 BACK 按钮终止的活动的结果?
采纳答案by alex2k8
I refactored my code. Initially I prepared some data and set it as activity result
in onDestroy
(this did not work). Now I set activity
data each time the data to be returned is updated, and have nothing in onDestroy
.
我重构了我的代码。起初,我准备了一些数据,并将其设置为activity result
在onDestroy
(这不工作)。现在activity
每次更新要返回的数据时我都会设置数据,并且onDestroy
.
回答by n224576
You need to overide the onBackPressed() method and set the result before the call to superclass, i.e
您需要覆盖 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();
}
回答by JBM
Activity
result must be set beforefinish()
is called. Clicking BACK actually calls finish()
on your activity
, so you can use the following snippet:
Activity
结果必须在finish()
调用之前设置。单击 BACK 实际上会调用finish()
您的activity
,因此您可以使用以下代码段:
@Override
public void finish() {
Intent data = new Intent();
setResult(RESULT_OK, data);
super.finish();
}
If you call NavUtils.navigateUpFromSameTask();
in onOptionsItemSelected()
, finish()
is called, but you will get the wrong result code
. So you have to call finish()
not navigateUpFromSameTask
in onOptionsItemSelected()
.
wrong requestCode in onActivityResult
如果你打电话NavUtils.navigateUpFromSameTask();
的onOptionsItemSelected()
,finish()
是所谓的,但你会得到错误的result code
。所以你必须调用finish()
not navigateUpFromSameTask
in onOptionsItemSelected()
。
onActivityResult 中的错误请求代码
回答by Khurram Shehzad
If you want to set some custom RESULT_CODE
in onBackPressed
event then you need to first set the result
and then call the super.onBackPressed()
and you will receive the same RESULT_CODE
in the caller activity's onActivityResult
method
如果你想RESULT_CODE
在onBackPressed
事件中设置一些自定义,那么你需要先设置result
然后调用super.onBackPressed()
,你将RESULT_CODE
在调用者活动的onActivityResult
方法中收到相同的
@Override
public void onBackPressed()
{
setResult(SOME_INTEGER);
super.onBackPressed();
}
回答by Ankur
Refer onActivityResult(int, int, Intent) doc
参考onActivityResult(int, int, Intent) 文档
Solution is to check the resultCodefor value Activity.RESULT_CENCELED. If yes, then it means that either BACK was pressed or the activity crashed. Hope it works for you guys, works for me :).
解决方案是检查resultCode的值Activity.RESULT_CENCELED。如果是,则表示按下 BACK 或活动崩溃。希望它对你们有用,对我有用:)。
回答by mdolanci
You should override onOptionsItemSelectedlike this:
你应该像这样覆盖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);
}
}
回答by banxi1988
i paste the answer may be will be helpful to other people: when a set launcheMode with android:launchMode="singleTask" i also cant get the result, the doc says:
我粘贴的答案可能对其他人有帮助:当使用 android:launchMode="singleTask" 设置 launcheMode 时,我也无法得到结果,文档说:
/* <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.
*/
回答by Bostone
Don't rely on any logic executed in onPause of one activity when you return to the initial one. According to the docs:
当您返回到初始活动时,不要依赖在一项活动的 onPause 中执行的任何逻辑。根据文档:
Implementations of this method (onPause) must be very quick because the next activity will not be resumed until this method returns
此方法 (onPause) 的实现必须非常快,因为在此方法返回之前不会恢复下一个活动
See http://goo.gl/8S2Yfor details.
有关详细信息,请参阅http://goo.gl/8S2Y。
The safest way is to set result after each result altering operation is complete (as you mention in your answer)
最安全的方法是在每个结果更改操作完成后设置结果(正如您在回答中提到的)
回答by MrJre
Try overriding onBackPressed (from android level 5 up), or override onKeyDown() and catch KeyEvent.BUTTON_BACK (see Android Activity Results) This does the trick for me.
尝试覆盖 onBackPressed(从 android 级别 5 开始),或覆盖 onKeyDown() 并捕获 KeyEvent.BUTTON_BACK(请参阅Android 活动结果)这对我有用。
回答by Roman Nurik
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
在链中为时已晚 - 而是覆盖onPause
并检查isFinishing()
以检查您的活动是否处于其生命周期的末尾。