onActivityResult executing twice

2019-02-18 01:50发布

问题:

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);

Here the implementation of onActivityResult method in HomeActivity:

@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);
            }
        }

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();
}**

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.

Why then is onActivityResult executing twice??

What is 0 on the first call and then 1 on the second??

Notes: I've read the following question to see if im doing something wrong but i can see it:

Android onActivityResult is always 0

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

And more..

UPDATE:

here my manifest

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

回答1:

You should try calling this in onResume() instead of onCreate();

Intent createProfile = new Intent(this, CreatePreacherActivity.class);
startActivityForResult(createProfile, 1);

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.



回答2:

Remove

super.onActivityResult(requestCode, resultCode, data);

from your activity's onActivityResult method.