Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 442
mcwadar Avatar asked Jul 26 '12 19:07

mcwadar


People also ask

What is the life cycle of Android activity?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .

What is Activity &describe activity life cycle in detail?

An activity is the single screen in android. It is like window or frame of Java. By the help of activity, you can place all your UI components or widgets in a single screen. The 7 lifecycle method of Activity describes how activity will behave at different states.

When onStop method is called in Android?

onStop() Called when the activity is no longer visible to the user. Either because another Activity has resumed, and is covering this one, an existing activity is coming to the foreground, or the activity is about to be destroyed.


2 Answers

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.

like image 74
coutol Avatar answered Sep 30 '22 06:09

coutol


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.

like image 44
Thomas Calc Avatar answered Sep 30 '22 05:09

Thomas Calc