简体   繁体   中英

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

please let me know why when i turn the screen off onAttach , onCreate ,.....etc are re-called?

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. You can always expect, if an app is not visible to the user, that Android may reclaim the resources that it takes.

You should always be prepared to handle lifecycle events and recover yourself when that happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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