Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - onConfigurationChanged() being called before onResume() after pausing Activity

Tags:

android

In my Activity, I override the onConfigurationChanged() method as it's playing a video, and I do not want to wait a second until the system is ready again to continue playing the video after I change my device's orientation. This works just fine - my video keeps playing smoothly and the orientation gets changed, while the video gets rescaled to just fit in the screen dimensions.

Yet I discovered a bug when these steps are taken:

  1. I pause my app during video playback
  2. I change orientation
  3. I "resume" my app

First thing what happens: onConfigurationChanged() gets called, not onResume() after which all my fields should be restored! This causes an Exception with a divide by zero as its cause - some field equals 0 as it's not restored yet. Ofcourse this is not desired behaviour. My app does work when resuming it with the same orientation it's paused in.

I can circumvent this, though, by using a boolean indicating whether my Activity has "fully loaded" - that is, the onResume() method has been called. But I consider this not a very clean solution.

My question is:

  1. Is this intended behaviour? I consider it weird that onConfigurationChanged() gets called before onResume(). Is this a bug?
  2. Can I programmatically "unregister" my app from listening to onConfigurationChanged() temporarily (for example, unregister it in the onPause() method)? As this method does not get called because you must register some listener to it, but gets called because you put in your AndroidManifest.xml file this line:

    android:configChanges="orientation"

like image 327
Timmos Avatar asked Nov 13 '22 11:11

Timmos


1 Answers

1- answer in http://developer.android.com/reference/android/app/Activity.html

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.

so its normal that onconfugrationchanged is called earlier to allow changing before starting activity again.. 2- I am not sure about this ..

like image 66
Maher Abuthraa Avatar answered Nov 16 '22 02:11

Maher Abuthraa