简体   繁体   English

在onDestroy之后,将重新调用生命周期回调

[英]after onDestroy, the life cycle callbacks are re-called

I have a main activity with viewpager and three tabs, each tab has its own fragment layout. 我有一个viewpager和三个选项卡的主要活动,每个选项卡都有自己的片段布局。 in each of the three fragment i implemented the life cycle call backs. 在这三个片段的每一个中,我都实现了生命周期回调。 The app was working fine till I pressed the the button that turns the screen off. 在我按下关闭屏幕的按钮之前,该应用程序运行良好。 when the screen turned off i found that after the the three fragments destroyed they got recreated again and onAttach , onCreate ,....etc were called again as well as shown in the logs posted below. 当屏幕关闭时,我发现在销毁了三个片段之后,它们又被重新创建,并且再次调用了onAttachonCreate ,...等,如下面发布的日志所示。

please let me know why when i turn the screen off onAttach , onCreate ,.....etc are re-called? 请让我知道为什么当我关闭屏幕onAttachonCreate ,.....等被重新调用时?

logcat : logcat

02-10 09:14:34.606 20109-20109/com.example.com.vpager_00 W/Frag_1: onPause()
02-10 09:14:34.606 20109-20109/com.example.com.vpager_00 W/Frag_2: onPause()
02-10 09:14:34.606 20109-20109/com.example.com.vpager_00 W/Frag_3: onPause()
02-10 09:14:34.606 20109-20109/com.example.com.vpager_00 W/MainActivity: onPause()
02-10 09:14:34.666 20109-20109/com.example.com.vpager_00 W/Frag_1: onStop()
02-10 09:14:34.666 20109-20109/com.example.com.vpager_00 W/Frag_2: onStop()
02-10 09:14:34.666 20109-20109/com.example.com.vpager_00 W/Frag_3: onStop()
02-10 09:14:34.666 20109-20109/com.example.com.vpager_00 W/MainActivity: onStop()
02-10 09:14:35.336 20109-20109/com.example.com.vpager_00 W/Frag_1: onDestroy()
02-10 09:14:35.336 20109-20109/com.example.com.vpager_00 W/Frag_2: onDestroy()
02-10 09:14:35.336 20109-20109/com.example.com.vpager_00 W/Frag_3: onDestroy()
02-10 09:14:35.336 20109-20109/com.example.com.vpager_00 W/MainActivity: onDestroy()
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 W/Frag_1: onAttach()
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 W/Frag_1: onCreate()
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 W/Frag_2: onAttach()
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 W/MainActivity: onFragRefHeld
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 W/Frag_2: onCreate()
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 W/Frag_3: onAttach()
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 V/Frag_3: getView(): null
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 W/Frag_3: onCreate()
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 V/Frag_3: getView(): null
02-10 09:14:35.456 20109-20109/com.example.com.vpager_00 W/MainActivity: onCreate()
02-10 09:14:35.466 20109-20109/com.example.com.vpager_00 W/Frag_1: onCreateView()
02-10 09:14:35.466 20109-20109/com.example.com.vpager_00 W/Frag_1: onViewCreated()
02-10 09:14:35.466 20109-20109/com.example.com.vpager_00 W/Frag_1: onActivityCreated()
02-10 09:14:35.466 20109-20109/com.example.com.vpager_00 W/Frag_2: onCreateView()
02-10 09:14:35.476 20109-20109/com.example.com.vpager_00 V/Frag_2: getView(): null
02-10 09:14:35.476 20109-20109/com.example.com.vpager_00 W/Frag_2: onViewCreated()
02-10 09:14:35.476 20109-20109/com.example.com.vpager_00 W/Frag_2: onActivityCreated()
02-10 09:14:35.476 20109-20109/com.example.com.vpager_00 W/Frag_3: onCreateView()
02-10 09:14:35.476 20109-20109/com.example.com.vpager_00 V/Frag_3: getView(): null
02-10 09:14:35.476 20109-20109/com.example.com.vpager_00 W/Frag_3: onViewCreated()  

This is normal. 这很正常。 When the screen turns off, Android may do whatever it wants with any activity the was visible in order to reclaim resources and help the device sleep while the screen is off. 当屏幕关闭时,Android可以执行任何可见的活动,以回收资源并帮助设备在屏幕关闭时进入睡眠状态。 You can always expect, if an app is not visible to the user, that Android may reclaim the resources that it takes. 您始终可以期望,如果用户看不到某个应用,则Android可能会回收它占用的资源。

You should always be prepared to handle lifecycle events and recover yourself when that happens. 您应该始终准备好处理生命周期事件,并在发生这种情况时恢复自己的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM