Reputation: 483
To solve some problems regarding App pause/resume, I tested the Activity Lifecycle by 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 a simple layout containing a Textview.
I installed the app and 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 the display off (by pushing the on/off button) and surprisingly saw the 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 I expected just to pause the app when I turn the display off, but why stop, destroy, create, start, resume and pause after it?
Upvotes: 1
Views: 74
Reputation: 16910
I think that your orientation changes when you are turning the display off. Most phones display the screenlock only in portrait mode. So before locking the screen, it rotates to orientation, and this configuration change is also applied to your app, which gets recreated because of this.
Try doing the same when your app is in portrait mode, it should work as expected.
To avoid recreating the activity on a configuration change, have a look at android:configChanges
.
Upvotes: 1
Reputation: 1077
Check you developer option in which in apps section there is option Don't keep activities if that is checked it will happen
Upvotes: 0