mcwadar
mcwadar

Reputation: 103

Android Activity Life Cycle in Jelly Bean

I have been developing an application for a few months now and getting ready to go to release. I have been using the Motorola Xoom with Android 4.0.4 for testing throughout the process and everything has worked without fail. I purchased a Nexus 7 from Google (running Jelly Bean 4.1) and I am now getting all kinds of problems with the activity life cycle when the device sleeps or turns off.

I created a new project for the sole purpose of checking the life cycle and this is the results I came up with. The following code is a sample of what I have done in each of the life cycle methods.

@Override
protected void onPause() {
    Log.i("TEST", "onPause()");
    super.onPause();
}

Results from Xoom (expected results)

---------- Program Launch -------------
onCreate(null)
onStart()
onResume()
---------- Turn Off Screen ------------
onPause()
onSaveInstanceState(not null)
onStop()
---------- Turn Screen On -------------
onReStart()
onRestoreInstanceState(not null)
onResume()

Results from Nexus 7 (unexpected results)

---------- Program Launch -------------
onCreate(null)
onStart()
onResume()
---------- Turn Off Screen ------------
onPause()
onSaveInstanceState(not null)
onStop()
onDestroy()
onCreate(not null)
onStart()
onRestoreInstanceState(not null)
onResume()
onPause()
---------- Turn Screen On -------------
OnResume()
onPause()
onSaveInstanceState(not null)
onStop()
onDestroy()
onCreate(not null)
onStart()
onRestoreInstanceState(not null)
onResume()

Again, these results are from a brand new project with no code changes other than the Log statements in each of the methods. Why are there so many additional, unnecessary method calls when the Nexus 7 gets turned off and back on? It seems to be completely destroying the application and then recreating.

For completeness sake, when using the home button and then relaunching the application, the life cycle seems to be consistent between devices.

Thanks in advance for any help. Wayne

Upvotes: 8

Views: 2051

Answers (2)

coutol
coutol

Reputation: 338

Jelly Bean 4.1 - Nexus 7 has one option on this path:

Settings -> Developer Options -> Don't keep activities...

That option will kill all activities that go to the background. So when Android change the status to 'sleeping' and 'waking up' your activity is being destroyed and re-created.

Upvotes: 6

Thomas Calc
Thomas Calc

Reputation: 3014

It looks like your Activity is re-created in the scenarios in question. This typically happens when a configuration change occurs in the system. Your Activity is re-created and restarted by default, unless you explicitly decide to handle the configuration change on your own. You should check this list, one of these changes might happen when you turn on/off your screen (as you sure you don't rotate the device at the same time?).

About configuration changes, you can read on this official page.

Upvotes: 4

Related Questions