我有一个活动,我只想存在一次,无论打开多少次。以前我使用launchMode singleTask,一切都很好。但是,现在我需要使用onActivityResult,由于某些原因,它与launchMode singleTask不兼容。防止活动的多个实例,但仍然使用onActivityResult
你知道一种方法来复制它们中的任何一个吗?
我有一个活动,我只想存在一次,无论打开多少次。以前我使用launchMode singleTask,一切都很好。但是,现在我需要使用onActivityResult,由于某些原因,它与launchMode singleTask不兼容。防止活动的多个实例,但仍然使用onActivityResult
你知道一种方法来复制它们中的任何一个吗?
我的主要活动也是一个单一的任务,但我肯定从我的主要活动开始活动结果并取回结果。
这里是我的舱单申报:
<activity
android:name=".activities.Main"
android:theme="@android:style/Theme.Light"
android:screenOrientation="portrait"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这里是我的onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
// blah blah checking if if the requestCode and resultCode are good //
}
我敢肯定,你设置的结果在其他活动......
所以,只需与我的代码交叉检查一下,看看我们是否能找到问题。
希望这有助于
-serkan
你说“确保onActivityResult是”保护“,当你压倒它”你能澄清?你的意思是说它不应该被宣布为私人? – Sojurn
您可以使用FLAG_ACTIVITY_REORDER_TO_FRONT,这将带来启动的活动给它的任务的堆栈的前面,而不是创建一个新的实例。
请注意,结果将传递到名为startActivityForResult的第一个活动。这answer提供了一个很好的例子,说明这是如何工作的。
“但是,现在我需要使用onActivityResult,它出于某种原因与onActivityResult不兼容” - 该句子无法正确解析。另外,为什么你认为你需要'singleTask',为什么你认为你需要'startActivityForResult()'''onActivityResult()'?如果你花时间解释你想要达到的目标,那么人们可以更容易地提出其他解决方案。 – CommonsWare
好的。我需要startActivityForResult()和onActivityResult(),因为我启动了一个细节视图,并需要从那里传播到我原来的活动。这个问题的基本前提是,我需要onActivityResult和singleTask完成的任务。我已经实现了一种解决方法(hack),其中我使用singleTask,但是使用Global Variables处理onResume中的活动。 – Leo
你为什么认为你需要'singleTask'? – CommonsWare