android - Weird Activity Lifecycle. -
when first start activity main activity, goes through oncreate() onstart(). while in app, if turn off screen, goes onpause() save data. goes onstop(), ondestroy(), again, without turning on screen oncreate(), onstart(), onpause(). should go oncreate(), onstart() right after ondestroy(), without me doing anything?
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_timegame); } @override protected void onresume() { super.onstart(); if (checkfirsttimerun()) initializegame(); else resumegame(); } @override protected void onpause() { super.onpause(); savegamedata(); }
shorter explanation(considering not using fragments
):
oncreate
,onstart
,onresume
when rotate screen onpause
,onstop
,ondestroy
- 1 time again oncreate
,onstart
,onresume
because activity destroyed , recreated onorientation change thus, if won't rotate oncreate
,onstart
,onresume
not fired sec time !
have @ 1 of answers posted in stackoverflow - click here
a utilize case example(considering using fragments
): sample project:
i have opensource project download here , execute , see log understand >the sequence of events executes 1 after another(i have fragment in project exclude >fragment logs if don't need it)
project observationsif path activity
-fragmentone
, alter orientation first time events fired follows mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
if path activity
-fragmentone
-orientationchange
, alter orientation first time events fired follows
mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onpause
- fragmentone-onsaveinstancestate
- fragmentone-onstop
- fragmentone-ondestroy
- fragmentone-ondetach
- mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
activity
-fragmentone
-orientationchange
-orientationchange
, alter orientation first time events fired follows mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onpause
- fragmentone-onsaveinstancestate
- fragmentone-onstop
- fragmentone-ondestroy
- fragmentone-ondetach
- mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onpause
- fragmentone-onsaveinstancestate
- fragmentone-onstop
- fragmentone-ondestroy
- fragmentone-ondetach
- mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
activity
-fragmentone
-orientationchange
-orientationchange
-fragmenttwo
, alter orientation first time events fired follows mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onpause
- fragmentone-onsaveinstancestate
- fragmentone-onstop
- fragmentone-ondestroy
- fragmentone-ondetach
- mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onpause
- fragmentone-onsaveinstancestate
- fragmentone-onstop
- fragmentone-ondestroy
- fragmentone-ondetach
- mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onstop
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmenttwo-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
activity
-fragmentone
-orientationchange
-orientationchange
-fragmenttwo
-orientationchange
, alter orientation first time events fired follows mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onpause
- fragmentone-onsaveinstancestate
- fragmentone-onstop
- fragmentone-ondestroy
- fragmentone-ondetach
- mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onpause
- fragmentone-onsaveinstancestate
- fragmentone-onstop
- fragmentone-ondestroy
- fragmentone-ondetach
- mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmentone-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onstop
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmenttwo-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
- fragmentone-onpause
- fragmentone-onsaveinstancestate
- fragmentone-onsaveinstancestate
- fragmentone-onstop
- fragmentone-ondestroy
- fragmentone-ondetach
- fragmentone-ondestroy
- fragmentone-ondetach
- mainactivity-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-onattach
- fragmentone-oncreate
- fragmentone-oncreateview
- fragmenttwo-onactivitycreated
- fragmentone-onstart
- fragmentone-onresume
android
No comments:
Post a Comment