Android - onConfigurationChanged () is called before onResume () after suspension

In my activity, I redefine the onConfigurationChanged () method when it plays the video, and I do not want to wait a second until the system is ready to continue playing the video again after changing the orientation of the device. This works very well - my video continues to play smoothly, and the orientation changes, and the video is scaled to just fit the screen size.

However, I found an error while completing these steps:

  • I pause my application while playing a video.
  • I change orientation
  • I am renewing my application

The first thing that happens: onConfigurationChanged () is called, not onResume (), after which all my fields must be restored! This throws an exception with division by zero as the reason - some field is 0, because it has not yet been restored. Of course, this is an undesirable behavior. My application really works when it resumes with the same orientation in which it stopped.

I can get around this, however, using a boolean value indicating whether my activity was "fully loaded", that is, the onResume () method was called. But I think this is not a very clean solution.

My question is:

  • Is this intentional behavior? I find it strange that onConfigurationChanged () is called before onResume (). This is mistake?
  • Can I programmatically β€œunregister” my application from listening to onConfigurationChanged () temporarily (for example, unregister in the onPause () method)? Since this method is not called because you have to register it as a listener, but it is called because you insert this line into your AndroidManifest.xml file:

    Android: configChanges = "orientation"

+4
source share
1 answer

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

In some special cases, you can get around restarting your own based on one or more types of configuration changes. This is done with the android: configChanges attribute in its manifest. For anyone, you say that you are dealing with this, you will receive a call to your current onConfigurationChanged activity, and not restarted. If the configuration change is due to the fact that you are not, however, the activity will still be restarted and onConfigurationChanged (Configuration) will not be called.

therefore its normal that onconfugrationchanged is called earlier to allow the change before starting the activity again. 2- I'm not sure about that.

+1
source

Source: https://habr.com/ru/post/1392951/


All Articles