onActivityResult执行两次

时间:2014-10-31 18:00:56

标签: android android-intent onactivityresult

来自 HomeActivity 我试图从 CreateProfileActivity 获得结果。这是我开始活动的方法

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 所做的工作:

**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获得执行,再次创建 OpenCrofileActivity ,因为第一次调用requestCode为0。

为什么onActivityResult会执行两次?

第一次通话时0是0,第二次是1是什么?

注意:我已阅读以下问题,看看我是否做错了但我能看到它:

Android onActivityResult is always 0

fragments startActivityForResult always return resultCode 0 and intent null on callback onActivityResult

And more..

更新:

这里是我的清单

<?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>

2 个答案:

答案 0 :(得分:2)

你应该尝试在onResume()而不是onCreate();

中调用它
Intent createProfile = new Intent(this, CreatePreacherActivity.class);
startActivityForResult(createProfile, 1);

Here,它说&#34;当您的活动重新启动时,您将在onResume()之前立即收到此电话。&#34;我相信第一次使用结果代码&#34; 0&#34;当您的第一个活动尝试恢复时触发。

答案 1 :(得分:1)

删除

super.onActivityResult(requestCode, resultCode, data);

来自您活动的onActivityResult方法。