onActivityResult不起作用? [英] onActivityResult doesn't work?

查看:111
本文介绍了onActivityResult不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临着一个问题相关startActivityForResult()

I am facing with a problem related startActivityForResult()

从FirstActivity开始SecondActivity:

To start SecondActivity from FirstActivity :

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

和拉手导致:

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

从SecondActivity将消息发送到FirstActivity:

To send the message to FirstActivity from SecondActivity :

在SecondActivity:

in SecondActivity :

setResult(0);

我不能处理在onActivityResult在FirstActivity结果。 它从来没有适​​合我的应用程序。

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

我的操作系统是:1.5

My OS is : 1.5

什么是错在这里?

请指教。

在此先感谢和放大器;新年快乐!

Thanks in advance & Happy new year!

推荐答案

startActivityForResult 是指用于要选择一个数据,或执行情况某种行动,你的活动或应用程序不能做什么。

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

例如,要选择一个联系人,这样你就启动联系人应用程序,用户选择他们想要的人,你会得到发送的结果。或者你想拍照,所以你启动相机应用程序,并要求它发送给您的照片一旦它完成。这个动作是调用 startActivityForResult

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.

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

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

所以,你的情况,你需要调用这 SecondActivity

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

setResult(...);
finish();

FirstActivity 将得到的结果在其 onActivityResult 方法。当然,这意味着 SecondActivity 现在已经没有了和 FirstActivity 是堆栈之上了。

before FirstActivity will receive the result in its onActivityResult method. Of course, this means that SecondActivity is now gone and FirstActivity is top of the stack again.

这是不可能的结果发送到 FirstActivity 然后将其关闭,同时保持 SecondActivity 仍然有效。在这种情况下,你应该处理什么这个结果是 SecondActivity ,或者把它送上一个服务您定义做任何处理是你想要的。

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

这篇关于onActivityResult不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆