当前位置: 首页 > 知识库问答 >
问题:

onActivityResult执行两次

禹德水
2023-03-14

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

当im完成并按下save inCreateProfileActivity后,下面是我将数据发送回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();
}**

CreateProfileActivity上调用setResult方法一次,但由于某种未知原因,当数据到达HomeActivity.OnActivityResult方法时,get execute两次。第一个结果的RequestCode=0和第二个结果的RequestCode=1。在此之后,HomeActivity.OnActivityResult得到execute之后,CreateProfileActivity再次执行,因为第一次调用requestCode是0。

那么为什么onActivityResult执行两次?

第一个电话是0,第二个电话是1是什么??

注意:我读了下面的问题,看看我是否做错了什么,但我能看到它:

更新:

这是我的舱单

<?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个答案

西门展
2023-03-14

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

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

在这里,它表示“当活动重新启动时,您将在onResume()之前立即收到此呼叫。”我认为第一次调用onActivityResult()(结果代码为“0”)是在尝试恢复第一个活动时触发的。

 类似资料:
  • 问题内容: 这是一个按钮: 和绑定事件: 一切正常,都发现一件麻烦事,我在Chrome开发者控制台中看到两个请求: 添加/ cartManager: 添加/ cartManager / add ?: 两者的请求标头几乎相同,只是请求标头不同: 第一个是 cartManager / add?pictureId = 等,第二个是 cartManager / add /?pictureId- / add

  • 问题内容: 我有一个从Qt Designer派生的简单窗口 (design.py) ,其中包含三个单选按钮: 并且我添加了此代码,以便监视选中了哪个单选按钮。 我注意到,如果选中radioButton1,它似乎可以正常工作,但是如果选中radiobutton2或radiobutton3,则将检查消息打印两次。 另一方面,如果我将每个信号都连接到不同的功能,例如: 然后它会按预期工作。 因此,我想将

  • 所以我不知道这是否真的描述了我的问题,但这是我能得到的最接近的吗? 我正在使用AWSAppsyncClient进行一些GraphQL突变。由于模型的性质,在更大程度上,由于我缺乏经验,我需要创建一个记录,然后创建两个依赖于第一个的记录,然后才能将它们链接到数据库中。 目前,我正在进行第一次变异,它返回所创建记录的ID。然后在查询返回的promise中创建中间记录。它基本上看起来像: 问题是Prom

  • 我想先除,然后乘,但不知道怎么做这两个想法? 它在工作,但它只是分裂的时刻。

  • 我是一个新的android和有以下查询。 我正在建立一个应用程序,其中包含两个活动第一和第二,发送消息给对方。“第一个”向“第二个”发送消息,然后“第二个”启动,显示接收到的消息,并向“第一个”发回消息。最后,首先显示收到的消息。 在第一个活动中,我必须使用startActivityForResult启动第二个活动,因为我需要返回响应。 谢了! PS-我已经尝试删除语句-if(resultCode

  • 问题内容: 我是Android开发的新手。我有一个问题。我尝试了最后几个小时,但我无法弄清楚。如果是这样,我有一个很普遍的问题。IllegalStateException:使用ViewPager在onSaveInstanceState之后无法执行此操作,但由于缺少Android开发经验而失败。 这是代码: 这是错误: 位于android.os.Handler.dispatchMessage(Han