jjzjj

android - onActivityResult 执行两次

coder 2023-12-20 原文

来自 HomeActivity 我正在尝试从 CreateProfileActivity 获取结果。这是我开始 Activity 的方法

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

更新:

这是我的 list

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

最佳答案

删除

super.onActivityResult(requestCode, resultCode, data);

来自您 Activity 的 onActivityResult 方法。

关于android - onActivityResult 执行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26680814/

有关android - onActivityResult 执行两次的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  3. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  4. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  5. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  6. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  7. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  8. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  9. ruby-on-rails - Rake 任务仅调用一次时执行两次 - 2

    我写了一个非常简单的rake任务来尝试找到这个问题的根源。namespace:foodotaskbar::environmentdoputs'RUNNING'endend当在控制台中执行rakefoo:bar时,输出为:RUNNINGRUNNING当我执行任何rake任务时会发生这种情况。有没有人遇到过这样的事情?编辑上面的rake任务就是写在那个.rake文件中的所有内容。这是当前正在使用的Rakefile。requireFile.expand_path('../config/application',__FILE__)OurApp::Application.load_tasks这里

  10. ruby-on-rails - 只有当不是 nil 时才执行映射? - 2

    如果names为nil,则以下中断。我怎样才能让这个map只有在它不是nil时才执行?self.topics=names.split(",").mapdo|n|Topic.where(name:n.strip).first_or_create!end 最佳答案 其他几个选项:选项1(在其上执行map时检查split的结果):names_list=names.try(:split,",")self.topics=names_list.mapdo|n|Topic.where(name:n.strip).first_or_create!e

随机推荐