android - Activity Lifecycle mismatch when turning off the display -
to solve problems regarding app pause/resume, tested activity lifecycle following simple app:
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.content_main); log.w("xxx", "create"); } @override public void onstart(){ super.onstart(); log.w("xxx","start"); } @override public void onresume(){ super.onresume(); log.w("xxx","resume"); } @override public void onpause(){ super.onpause(); log.w("xxx","pause"); } @override public void ondestroy(){ super.ondestroy(); log.w("xxx","destroy"); } @override public void onstop(){ super.onstop(); log.w("xxx","stop"); } @override public void onrestart(){ super.onrestart(); log.w("xxx","restart"); } }
with simple layout containing textview.
i installed app , ran it:
01-28 11:56:09.032 2517-2517/android.se.behy.test w/xxx: create 01-28 11:56:09.035 2517-2517/android.se.behy.test w/xxx: start 01-28 11:56:09.035 2517-2517/android.se.behy.test w/xxx: resume
and turned display off (by pushing on/off button) , surprisingly saw following log:
01-28 11:56:09.032 2517-2517/android.se.behy.test w/xxx: create 01-28 11:56:09.035 2517-2517/android.se.behy.test w/xxx: start 01-28 11:56:09.035 2517-2517/android.se.behy.test w/xxx: resume 01-28 11:56:20.750 2517-2517/android.se.behy.test w/xxx: pause 01-28 11:56:20.753 2517-2517/android.se.behy.test w/xxx: stop 01-28 11:56:20.809 2517-2517/android.se.behy.test w/xxx: destroy 01-28 11:56:20.843 2517-2517/android.se.behy.test w/xxx: create 01-28 11:56:20.844 2517-2517/android.se.behy.test w/xxx: start 01-28 11:56:20.844 2517-2517/android.se.behy.test w/xxx: resume 01-28 11:56:20.857 2517-2517/android.se.behy.test w/xxx: pause
question
normally expected pause app when turn display off, why stop, destroy, create, start, resume , pause after it?
i think orientation changes when turning display off. phones display screenlock in portrait mode. before locking screen, rotates orientation, , configuration change applied app, gets recreated because of this.
try doing same when app in portrait mode, should work expected.
to avoid recreating activity on configuration change, have @ android:configchanges
.
Comments
Post a Comment