关于片段生命周期的困惑
confusion regarding fragments' lifeCycle
我有一个托管 ViewPager 并包含三个选项卡(MQTT、ECO、记录器)的 mainActivity。 mainActivity 具有生命周期回调(onCreat、onResume),其他三个选项卡具有 Fragment'
生命周期回调(onattached、onCreateView、onViewcreated、onResume、onPause)。
在 运行 时间,我在 logCat 收到以下内容:
01-29 13:28:28.732: W/MainActivity(31412): @onCreate().
01-29 13:28:28.802: W/MainActivity(31412): @onResume().
01-29 13:28:28.842: W/MQTT_Settings_Frag(31412): @onAttach().
01-29 13:28:28.842: W/MQTT_Settings_Frag(31412): @onCreateView().
01-29 13:28:28.862: W/MQTT_Settings_Frag(31412): @onViewCreated().
01-29 13:28:28.862: W/MQTT_Settings_Frag(31412): @onResume().
01-29 13:28:28.862: W/Eco_Settings_Frag(31412): @onAttach().
01-29 13:28:28.862: W/Eco_Settings_Frag(31412): @onCreateView().
01-29 13:28:28.892: W/Eco_Settings_Frag(31412): @onViewCreated().
01-29 13:28:28.892: W/Eco_Settings_Frag(31412): @onResume().
当我滑动到 Eco-tab "the second one" 时,logCa 只报告碎片,但是当我滑动到第三个选项卡时 "Logger"-
尚未实施- logCat 显示:
01-29 13:37:27.062: W/MQTT_Settings_Frag(31412): @onPause().
当我从 Logger-tab 返回 Eco-Tab 时,LogCat 显示以下内容:
01-29 13:37:52.292: W/MQTT_Settings_Frag(31412): @onCreateView().
01-29 13:37:52.322: W/MQTT_Settings_Frag(31412): @onViewCreated().
01-29 13:37:52.322: W/MQTT_Settings_Frag(31412): @onResume().
why the first tab "MQTT-Tab" goes in onPause() when i goto only to the third Tab Logger-Tab?
And why the first tab "MQTT-Tab" goes into (onCreateView, onViewCreated, onResume) when i move to the secondtab Eco-tab frm the third one LoggerTab?
ViewPager 默认只在内存中保留相邻的选项卡,因此当您转到第三个选项卡时,您的第一个选项卡会被销毁,并在您返回中间时重新创建。您可以使用 yourViewPager.setOffscreenPageLimit(int)
.
更改此行为
我有一个托管 ViewPager 并包含三个选项卡(MQTT、ECO、记录器)的 mainActivity。 mainActivity 具有生命周期回调(onCreat、onResume),其他三个选项卡具有 Fragment' 生命周期回调(onattached、onCreateView、onViewcreated、onResume、onPause)。
在 运行 时间,我在 logCat 收到以下内容:
01-29 13:28:28.732: W/MainActivity(31412): @onCreate().
01-29 13:28:28.802: W/MainActivity(31412): @onResume().
01-29 13:28:28.842: W/MQTT_Settings_Frag(31412): @onAttach().
01-29 13:28:28.842: W/MQTT_Settings_Frag(31412): @onCreateView().
01-29 13:28:28.862: W/MQTT_Settings_Frag(31412): @onViewCreated().
01-29 13:28:28.862: W/MQTT_Settings_Frag(31412): @onResume().
01-29 13:28:28.862: W/Eco_Settings_Frag(31412): @onAttach().
01-29 13:28:28.862: W/Eco_Settings_Frag(31412): @onCreateView().
01-29 13:28:28.892: W/Eco_Settings_Frag(31412): @onViewCreated().
01-29 13:28:28.892: W/Eco_Settings_Frag(31412): @onResume().
当我滑动到 Eco-tab "the second one" 时,logCa 只报告碎片,但是当我滑动到第三个选项卡时 "Logger"- 尚未实施- logCat 显示:
01-29 13:37:27.062: W/MQTT_Settings_Frag(31412): @onPause().
当我从 Logger-tab 返回 Eco-Tab 时,LogCat 显示以下内容:
01-29 13:37:52.292: W/MQTT_Settings_Frag(31412): @onCreateView().
01-29 13:37:52.322: W/MQTT_Settings_Frag(31412): @onViewCreated().
01-29 13:37:52.322: W/MQTT_Settings_Frag(31412): @onResume().
why the first tab "MQTT-Tab" goes in onPause() when i goto only to the third Tab Logger-Tab?
And why the first tab "MQTT-Tab" goes into (onCreateView, onViewCreated, onResume) when i move to the secondtab Eco-tab frm the third one LoggerTab?
ViewPager 默认只在内存中保留相邻的选项卡,因此当您转到第三个选项卡时,您的第一个选项卡会被销毁,并在您返回中间时重新创建。您可以使用 yourViewPager.setOffscreenPageLimit(int)
.