I spent one whole afternoon on a very silly issue about the Android activity lifecyle when the focus changes and the device is turned on. I want to share it with you to avoid you some hours of investigation!
Context
In my Stopeen Android app, sometimes I launched an alarm (ringtone and vibrations). When a ringing alarm was ongoing (i.e. the activity AlarmActivity was create), pressing the Home button stopped the alarm (i.e. the activity killed itself). In order to do that, I overrode the onPause method, supposed to be called when the app loose the focus. When onPause was called, the activity killed itself.
On an old device with Android 2.3, it worked perfectly fine, but when I tested on Android 4.4, a strange behavior happened. When my alarm was launched and the device screen was off, the device woke up (expected behavior) but the alarm stopped after half of a second (unexpected behavior). This did not happen if the screen device was already turned, only if it was off.
Hey clever boy! This bug surely came from the PendingIntent you used with the AlarmManager, or from the Wakelock you relied on, or from the way you used the WindowManager to turn on the screen.
Of course I thought that at the beginning, but actually it was none of them: it was the fault of the Activity default lifecycle.
The proof
To detect that, I cleaned almost all my AlarmActivity and added logs in the lifecycle methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
protected void onCreate(Bundle savedInstanceState) { Log.i("TEST","onCreate()"); super.onCreate(savedInstanceState); // Force the screen to turn on when the activity is created this.getWindow().setFlags( WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); } protected void onPause() { Log.i("TEST","onPause()"); super.onPause(); } |
Do you see the simple log in onPause? Actually I did the same for onStart, onRestart, onResume, onStop and onDestroy.
Here is the result I got on a Samsung Galaxy S5 mini (Android 4.4.2):
When the screen is on | When the screen is off | ||||
---|---|---|---|---|---|
|
|
||||
|
|
WTF?! Why is the activity started, stopped and then restarted when the screen goes from off to on?
Actually I don’t know, it is not very logical, both cases should be the same. Instead, when the device is turned on, the lifecycle starts very differently. In my case, I was killing the activity when onPause was called, so you can clearly see that when doing that, my activity is creating (onCreate) and killed (onPause) in the same cycle.
Conclusion: alternative to onPause to handle focus loss
After a while I found on this post that the Android default lifecycle has changed between Android 4.0 and Android 4.1.
So instead of using onPause to handle focus loss, now I use onWindowFocusChanged. It works perfectly fine, even if the screen is off.
By Curry Shoes Black,Curry Shoes Review,Curry Shoes 2014,Curry Shoes Foot Locker,Curry Shoes Lexington Ky,Curry Shoes Size 7,Curry Shoes Size 8,Curry Shoes White,Curry Shoes Nike,Curry Shoes Under Armour,Curry Shoes Blue,Curry Shoes Bury St Edmunds,Curry Bas 08/08/2015 - 06:52
Ηello, I enjoy reading all of your article.
I wanted tо wrіte a little comment to support yοu.