【问题标题】:onActivityResult executing twiceonActivityResult 执行两次
【发布时间】:2018-04-12 21:55:21
【问题描述】:

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

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

这里是HomeActivityonActivityResult方法的实现:

@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 中按 save 后,我将数据发送回 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。

为什么 onActivityResult 会执行两次?

什么是第一次调用时为 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>

【问题讨论】:

  • 你能告诉我们你的清单吗?你设置singleTask了吗?
  • @PedroOliveira 现在看看。如果那是错误。我该如何解决??
  • @Misters 你能尝试从“CurrentUser.set(this,u);”中删除“this”吗?看起来“CreateProfileActivity”活动的上下文在完成后会返回到 onActivityResult。
  • @kozaxinan this 关键字作为参数传递是只能访问数据库,仅此而已。它是一个静态类,可以在内存中保存一些数据
  • 可以在 onResume 中试试吗?

标签: android android-intent onactivityresult


【解决方案1】:

删除

super.onActivityResult(requestCode, resultCode, data);

来自您的活动的 onActivityResult 方法。

【讨论】:

    【解决方案2】:

    您应该尝试在 onResume() 而不是 onCreate() 中调用它;

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

    Here,它说“当您的活动重新开始时,您将在 onResume() 之前立即收到此调用。”我相信当您的第一个活动尝试恢复时,第一次调用 onActivityResult() 结果代码为“0”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-27
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 2018-10-02
      • 1970-01-01
      相关资源
      最近更新 更多