onActivityResult执行两次 [英] onActivityResult executing twice
问题描述
来自 HomeActivity ,我试图从 CreateProfileActivity 获取结果.这里是我开始活动的方式
From HomeActivity Im trying to get a result from CreateProfileActivity. Here what i do to start the activity
Intent createProfile = new Intent(this, CreatePreacherActivity.class);
startActivityForResult(createProfile, 1);
在 HomeActivity 中实现onActivityResult
方法的实现:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1)
{
Log.d("DEV","HomeActivity is reciving data");
if (resultCode == RESULT_OK)
{
// this code here execute at the second call
// but why there are two calls when i just call setResult once???
User user = new User();
String[] userInfo = data.getStringArrayExtra("preacher");
//parsing and saving and database code...
Log.d("DEV","HomeActivity data received->"+user);
CurrentUser.set(this, user);
fillCurrentUserInforms();
}
// if while searching for the preacher list
// and none was found on database. Then tell the user
// to create a new profile
else if (resultCode == RESULT_CANCELED)
{
Intent createPreacherIntent = new Intent( this, CreatePreacherActivity.class );
startActivityForResult(createPreacherIntent,1);
}
}
完成后,按CreateProfileActivity
中的保存,这是我将数据发送回 HomeActivity 的方法:
When im done and press save in CreateProfileActivity
here is what I do to send the data back to HomeActivity:
**private void createProfile()
{
// some parsing and inserting the new data code..
User u = new User();
u.setName(newPreacher.getName());
u.setLastName(newPreacher.getLastName());
u.setId(newPreacher.getId());
u.setSex(newPreacher.getSex());
CurrentUser.set(this,u);
if (getParent() == null)
{
setResult(RESULT_OK,createPreacherDataIntent(newPreacher));
}
else
{
getParent().setResult(RESULT_OK,createPreacherDataIntent(newPreacher));
}
Log.d("DEV","Exiting from CreatePreacherActivity");
finish();
}**
setResult方法在CreateProfileActivity
上被调用一次,但是由于某种未知的原因,当数据到达HomeActivity.onActivityResult
方法时,将执行两次.第一个结果为requestCode = 0
,第二个结果为requestCode = 1
.之后,HomeActivity.onActivityResult
开始执行, CreateProfileActivity再次终止,因为第一次调用requestCode为0.
The setResult method is call one once on CreateProfileActivity
but for some unknown reason when the data get to HomeActivity.onActivityResult
method, get execute twice. The first result with the requestCode = 0
and the second one with requestCode = 1
. After that, HomeActivity.onActivityResult
gets execute, the CreateProfileActivity apears again since the first call the requestCode was 0.
为什么onActivityResult执行两次?
第一个电话是多少,然后第二个电话是1?
注意:我已阅读以下问题,以查看即时消息是否做错了什么,但我可以看到它:
Notes: I've read the following question to see if im doing something wrong but i can see it:
片段startActivityForResult始终返回resultCode 0和回调onActivityResult上的intent为null
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jwutils.einers.informedeserviciotj" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activities.HomeActivity"
android:label="Inform de Servicio" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.inform.InformEditableActivity"></activity>
<activity android:name=".Activities.preacher.CreatePreacherActivity"
android:label="Crear publicador"/>
<activity android:name=".Activities.preacher.PreachersListActivity"
android:label="Usuarios" />
</application>
</manifest>
推荐答案
您应该尝试使用onResume()而不是onCreate();
You should try calling this in onResume() instead of onCreate();
Intent createProfile = new Intent(this, CreatePreacherActivity.class);
startActivityForResult(createProfile, 1);
此处,它说:当您的活动重新开始时,您将在onResume()之前立即收到此呼叫."我相信当您的第一个活动尝试恢复时,将触发结果代码为"0"的onActivityResult()的首次调用.
Here, it say "You will receive this call immediately before onResume() when your activity is re-starting." I beleive that the first call of onActivityResult() with result code "0" is triggered when your first activity try to resume.
这篇关于onActivityResult执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!