Android 如何从 TabHost 活动返回结果 (startActivityForResult)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2497205/
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
How to return a result (startActivityForResult) from a TabHost Activity?
提问by Cameron McBride
I have 3 classes in my example: Class A, the main activity. Class A calls a startActivityForResult:
我的示例中有 3 个类:A 类,主要活动。A 类调用一个 startActivityForResult:
Intent intent = new Intent(this, ClassB.class);
startActivityForResult(intent, "STRING");
Class B, this class is a TabActivity:
B类,这个类是一个TabActivity:
Intent intent = new Intent(this, ClassC.class);
tabHost.addTab...
Class C, this class is a regular Activity:
C类,这个类是一个常规的Activity:
Intent intent = this.getIntent();
intent.putExtra("SOMETHING", "EXTRAS");
this.setResult(RESULT_OK, intent);
finish();
onActivityResult is called in Class A, but the resultCode is RESULT_CANCELEDinstead of RESULT_OKand the returned intent is null. How do I return something from the Activity inside a TabHost?
在A类中调用onActivityResult,但是resultCode是RESULT_CANCELED而不是,RESULT_OK返回的intent为null。如何从 TabHost 内的 Activity 返回某些内容?
I realize that the problem is that my Class C is actually running inside of Class B, and Class B is what is returning the RESULT_CANCELEDback to Class A. I just don't know a work around yet.
我意识到问题是我的 C 类实际上在 B 类内部运行,而 B 类是返回RESULT_CANCELED到 A 类的东西。我只是不知道解决方法。
回答by Ilya Taranov
Oh, god! After spending several hours and downloading the Android sources, I have finally come to a solution.
天啊!在花了几个小时下载了 Android 源代码之后,我终于找到了解决方案。
If you look at the Activity class, you will see, that finish()method only sends back the result if there is a mParentproperty set to null. Otherwise the result is lost.
如果您查看 Activity 类,您会看到,该finish()方法仅在将mParent属性设置为时才发回结果null。否则结果丢失。
public void finish() {
if (mParent == null) {
int resultCode;
Intent resultData;
synchronized (this) {
resultCode = mResultCode;
resultData = mResultData;
}
if (Config.LOGV) Log.v(TAG, "Finishing self: token=" + mToken);
try {
if (ActivityManagerNative.getDefault()
.finishActivity(mToken, resultCode, resultData)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild(this);
}
}
So my solution is to set result to the parent activity if present, like that:
所以我的解决方案是将结果设置为父活动(如果存在),如下所示:
Intent data = new Intent();
[...]
if (getParent() == null) {
setResult(Activity.RESULT_OK, data);
} else {
getParent().setResult(Activity.RESULT_OK, data);
}
finish();
I hope that will be helpful if someone looks for this problem workaround again.
如果有人再次寻找此问题的解决方法,我希望这会有所帮助。
回答by Dave S
http://tylenoly.wordpress.com/2010/10/27/how-to-finish-activity-with-results/
http://tylenoly.wordpress.com/2010/10/27/how-to-finish-activity-with-results/
With a slight modification for "param_result"
对“param_result”稍作修改
/* Start Activity */
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.thinoo.ActivityTest", "com.thinoo.ActivityTest.NewActivity");
startActivityForResult(intent,90);
}
/* Called when the second activity's finished */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case 90:
if (resultCode == RESULT_OK) {
Bundle res = data.getExtras();
String result = res.getString("param_result");
Log.d("FIRST", "result:"+result);
}
break;
}
}
private void finishWithResult()
{
Bundle conData = new Bundle();
conData.putString("param_result", "Thanks Thanks");
Intent intent = new Intent();
intent.putExtras(conData);
setResult(RESULT_OK, intent);
finish();
}
回答by onlythoughtworks
Intent.FLAG_ACTIVITY_FORWARD_RESULT?
Intent.FLAG_ACTIVITY_FORWARD_RESULT?
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity.
如果设置并且此意图用于从现有活动启动新活动,则现有活动的回复目标将转移到新活动。
回答by Prashast
You could implement a onActivityResult in Class B as well and launch Class C using startActivityForResult. Once you get the result in Class B then set the result there (for Class A) based on the result from Class C. I haven't tried this out but I think this should work.
您也可以在 B 类中实现 onActivityResult 并使用 startActivityForResult 启动 C 类。一旦你在 B 类中得到结果,然后根据 C 类的结果在那里设置结果(对于 A 类)。我没有试过这个,但我认为这应该可行。
Another thing to look out for is that Activity A should not be a singleInstance activity. For startActivityForResult to work your Class B needs to be a sub activity to Activity A and that is not possible in a single instance activity, the new Activity (Class B) starts in a new task.
需要注意的另一件事是 Activity A 不应是 singleInstance 活动。要使 startActivityForResult 工作,您的 B 类需要成为 Activity A 的子活动,而这在单个实例活动中是不可能的,新活动(B 类)在新任务中启动。
回答by App-SoftwareFactory
For start Activity 2 from Activity 1 and get result, you could use startActivityForResult and implement onActivityResult in Activity 1 and use setResult in Activity2.
对于从 Activity 1 启动 Activity 2 并获得结果,您可以使用 startActivityForResult 并在 Activity 1 中实现 onActivityResult 并在 Activity2 中使用 setResult。
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(NUMERO1, numero1);
intent.putExtra(NUMERO2, numero2);
//startActivity(intent);
startActivityForResult(intent, MI_REQUEST_CODE);

