如何从TabHost活动返回结果(startActivityForResult)? [英] How to return a result (startActivityForResult) from a TabHost Activity?
问题描述
我有3个班在我的例子: A类,主要活动。 A类调用startActivityForResult:
I have 3 classes in my example: Class A, the main activity. Class A calls a startActivityForResult:
Intent intent = new Intent(this, ClassB.class);
startActivityForResult(intent, "STRING");
B类,这个类是一个TabActivity:
Class B, this class is a TabActivity:
Intent intent = new Intent(this, ClassC.class);
tabHost.addTab...
C类,这个类是一个普通的活动:
Class C, this class is a regular Activity:
Intent intent = this.getIntent();
intent.putExtra("SOMETHING", "EXTRAS");
this.setResult(RESULT_OK, intent);
finish();
onActivityResult被称为A级,但结果code是 RESULT_CANCELED
而不是 RESULT_OK
和返回的意图是空。我如何返回的东西从里面TabHost的活动?
onActivityResult is called in Class A, but the resultCode is RESULT_CANCELED
instead of RESULT_OK
and the returned intent is null. How do I return something from the Activity inside a TabHost?
我意识到,问题是,我的C类实际上是B级的内部运行,而B类是什么返回 RESULT_CANCELED
返回到A级。我刚不知道工作尚未各地。
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_CANCELED
back to Class A. I just don't know a work around yet.
推荐答案
哦,上帝!花几个小时,下载了Android源后,我终于得出一个解决方案。
Oh, god! After spending several hours and downloading the Android sources, I have finally come to a solution.
如果你看一下Activity类,你会看到,完成()
方法只发回的结果,如果有一个 mParent
属性设置为空
。否则结果将丢失。
If you look at the Activity class, you will see, that finish()
method only sends back the result if there is a mParent
property set to null
. Otherwise the result is lost.
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);
}
}
所以我的解决办法是将结果向父活动,如果present,这样的:
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.
这篇关于如何从TabHost活动返回结果(startActivityForResult)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!