草庐IT

android - Android 方向改变时执行哪些Activity 的生命周期回调?

coder 2023-12-11 原文

我在调试涉及方向更改和一些空返回的情况时卡住了。

我的问题分为:

1) 当方向改变时,Activity 生命周期的回调会被执行。

2) 当方向改变时,Fragment 的生命周期回调会被执行。

3) 如果我们合并第 2 点和第 3 点(这通常是 fragment 覆盖 Activity 的情况),根据具有 fragment 的 Activity(执行流程?),回调执行流程将是什么。

编辑

如果在 android list 文件中没有定义 configChanges 即:

If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).

Activity 和 Fragment 将执行哪些生命周期回调

最佳答案

首先,在 AndroidManifest 中将其添加到您的 Activity 中:android:configChanges="orientation"

1). onConfigurationChanged
2).没什么,但是您可以实现您的方法并从 Activity 的 onConfigurationChanged 中调用它
3).流程将是 Activity.onConfigurationChanged -> Fragment.yourMethod

希望这对您有所帮助。

已更新

这是您第一次开始 Activity 时的流程:

Activity.onCreate
Activity.onStart
Fragment.onAttach
Fragment.onCreate
Fragment.onCreateView
Fragment.onViewStateRestored
Fragment.onStart
Activity.onResume
Fragment.onResume

这是方向改变后的流程:

Activity.onPause
Fragment.onPause
Activity.onSaveInstanceState
Fragment.onSaveInstanceState
Activity.onStop
Fragment.onStop
Activity.onDestroy
Fragment.onDestroy
Fragment.onDetach
Fragment.onAttach
Fragment.onCreate
Activity.onCreate
Activity.onStart
Fragment.onCreateView
Fragment.onViewStateRestored
Fragment.onStart
Activity.onRestoreInstanceState
Activity.onResume
Fragment.onResume

检查代码如下:

public class FooActivity extends FragmentActivity {

    private static final String LOG_TAG = FooActivity.class.getSimpleName() + "_TAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(LOG_TAG, "Activity.onCreate");
        setContentView(R.layout.activity_foo);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new BarFragment())
                    .commit();
        }
    }


    @Override
    protected void onPause() {
        Log.i(LOG_TAG, "Activity.onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.i(LOG_TAG, "Activity.onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.i(LOG_TAG, "Activity.onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        Log.i(LOG_TAG, "Activity.onResume");
        super.onResume();
    }

    @Override
    protected void onStart() {
        Log.i(LOG_TAG, "Activity.onStart");
        super.onStart();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.i(LOG_TAG, "Activity.onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.i(LOG_TAG, "Activity.onSaveInstanceState");
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        Log.i(LOG_TAG, "Activity.onRestoreInstanceState");
        super.onRestoreInstanceState(savedInstanceState);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class BarFragment extends Fragment {

        public BarFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Log.i(LOG_TAG, "Fragment.onCreateView");
            View rootView = inflater.inflate(R.layout.fragment_bar, container, false);
            return rootView;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            Log.i(LOG_TAG, "Fragment.onCreate");
            super.onCreate(savedInstanceState);
        }

        @Override
        public void onAttach(Activity activity) {
            Log.i(LOG_TAG, "Fragment.onAttach");
            super.onAttach(activity);
        }

        @Override
        public void onViewStateRestored(Bundle savedInstanceState) {
            Log.i(LOG_TAG, "Fragment.onViewStateRestored");
            super.onViewStateRestored(savedInstanceState);
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            Log.i(LOG_TAG, "Fragment.onSaveInstanceState");
            super.onSaveInstanceState(outState);
        }

        @Override
        public void onResume() {
            Log.i(LOG_TAG, "Fragment.onResume");
            super.onResume();
        }

        @Override
        public void onStart() {
            Log.i(LOG_TAG, "Fragment.onStart");
            super.onStart();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            Log.i(LOG_TAG, "Fragment.onConfigurationChanged");
            super.onConfigurationChanged(newConfig);
        }

        @Override
        public void onPause() {
            Log.i(LOG_TAG, "Fragment.onPause");
            super.onPause();
        }

        @Override
        public void onStop() {
            Log.i(LOG_TAG, "Fragment.onStop");
            super.onStop();
        }

        @Override
        public void onDetach() {
            Log.i(LOG_TAG, "Fragment.onDetach");
            super.onDetach();
        }

        @Override
        public void onDestroy() {
            Log.i(LOG_TAG, "Fragment.onDestroy");
            super.onDestroy();
        }
    }
}

关于android - Android 方向改变时执行哪些Activity 的生命周期回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31338683/

有关android - Android 方向改变时执行哪些Activity 的生命周期回调?的更多相关文章

  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 - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  4. ruby - 有人可以帮助解释类创建的 post_initialize 回调吗 (Sandi Metz) - 2

    我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法

  5. 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,实际上您甚至打印它。试试

  6. 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-检查是否

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

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

  9. 安卓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,打开命令窗口,并将路

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

随机推荐