Android onActivityResult 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1984233/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:04:15  来源:igfitidea点击:

onActivityResult doesn't work?

android

提问by AndroiDBeginner

I am facing with a problem related startActivityForResult()

我面临与 startActivityForResult() 相关的问题

To start SecondActivity from FirstActivity :

从 FirstActivity 启动 SecondActivity :

Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("key1", "12345");
startActivityForResult(intent, 0);

And handles result :

并处理结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //TODO handle here. 
}

To send the message to FirstActivity from SecondActivity :

要将消息从 SecondActivity 发送到 FirstActivity :

in SecondActivity :

在第二个活动中:

setResult(0);

I can't handle the result on onActivityResult in FirstActivity. It never works for my application.

我无法处理 FirstActivity 中 onActivityResult 的结果。它永远不适用于我的应用程序。

My OS is : 1.5

我的操作系统是:1.5

What is wrong here?

这里有什么问题?

回答by Christopher Orr

startActivityForResultis meant to be used for situations where you want to select a piece of data, or perform some sort of action that your Activityor application cannot do.

startActivityForResult旨在用于您想要选择一段数据或执行某种您Activity或应用程序无法执行的操作的情况。

For example, you want to pick a contact, so you launch the contacts application, the user chooses the person they want, and you get sent the result. Or you want to take a photo, so you launch the camera application and ask it to send you the photo once it's done. This action is completely separate from your first activity that calls startActivityForResult.

例如,您想选择一个联系人,因此您启动了联系人应用程序,用户选择了他们想要的人,然后您就会收到结果。或者您想拍照,因此您启动相机应用程序并要求它在完成后将照片发送给您。此操作与调用 的第一个活动完全分开startActivityForResult

The Activityyou're launching will not send you the result until that Activityhas completed, i.e. finish()has been called.

Activity直到你推出不会给你的结果Activity已经完成,即finish()已经被调用。

So in your case, you need to call this in SecondActivity:

所以在你的情况下,你需要调用它SecondActivity

setResult(...);
finish();

before FirstActivitywill receive the result in its onActivityResultmethod. Of course, this means that SecondActivityis now gone and FirstActivityis top of the stack again.

beforeFirstActivity将在其onActivityResult方法中接收结果。当然,这意味着SecondActivity现在已经消失并FirstActivity再次成为堆栈顶部。



It's not possible to send the result to FirstActivitythen close it while keeping SecondActivitystill active. In this case you should just handle whatever this 'result' is in SecondActivity, or send it off to a Serviceyou define to do whatever processing it is you want.

FirstActivity在保持SecondActivity活动状态的同时发送结果然后关闭它是不可能的。在这种情况下,您应该只处理这个“结果”中的任何内容SecondActivity,或者将其发送到Service您定义的 a 以执行您想要的任何处理。

回答by matangs

I was stuck here for a while. Adding my problem here to make sure that you don't scratch your head as well.

我被困在这里了一段时间。在这里添加我的问题以确保您也不会挠头。

The second parameter of this function has to be 0 or higher.

此函数的第二个参数必须为 0 或更高。

startActivityForResult(intent, 0); // <- this is OK

I was setting the second parameter to RESULT_OK, which is -1, and my onActivityResult callback was never getting called. So if you get stuck like me, you can also check if your second parameter is correct.

我将第二个参数设置为 RESULT_OK,即 -1,并且我的 onActivityResult 回调从未被调用。所以如果你像我一样卡住了,你也可以检查你的第二个参数是否正确。

startActivityForResult(intent, RESULT_OK); // <- this is wrong

The above line will fail to call onActivityResult.

上面的行将无法调用 onActivityResult。

回答by johndodo

I was also stuck on the same problem - but due to a different reason as matangs. Apparently startActivityForResultonly works if you have android:launchModeset to standardfor main activity (in manifest). Hope it helps someone.

我也遇到了同样的问题 - 但由于与matangs不同的原因。显然startActivityForResult只有在您android:launchMode设置standard为主要活动(在清单中)时才有效。希望它可以帮助某人。

回答by tbruyelle

Your code seems ok, but do you stop your second activity ?

您的代码似乎没问题,但是您是否停止了第二项活动?

Try this in it :

试试这个:

setResult(0);
finish();

回答by NBApps

If you are doing actions on onPause (like unbinding a service) try to comment it and see if onActivityResult is called (I wasted few good hours on this..)

如果您正在对 onPause 执行操作(例如取消绑定服务),请尝试对其进行评论并查看是否调用了 onActivityResult(我在这方面浪费了几个小时……)

回答by Shai Epstein

Thanks to @johndodo (that point to the manifiest) - I find my solution for the same problem.

感谢@johndodo(指向清单) - 我找到了同样问题的解决方案。

removing android:noHistory=trueat the manifiest" solved this problem for me.

android:noHistory=true在清单中删除”为我解决了这个问题。