0

If we do not want to restart the activity during config changes, we can set flag android:configChanges;

If we need to restart the activity (i.e., to update resources), we should not set the flag.

In what situations, do we need to set flag android:configChanges while overriding the callback onConfigurationChanged()?

JackWM
  • 10,085
  • 22
  • 65
  • 92

1 Answers1

2

When you set android:configChanges in the manifest, that means you are telling the system that you will handle the configuration change yourself manually in the Activity.

From the doc of android:configChanges:

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

So if you set the configChanges in the manifest, you have to override the onConfigurationChanged() callback and handle the change. Otherwise the system will handle the change automatically -- like loading a proper resource -- and you don't have to override the onConfigurationChanged() callback.

Also from the doc:

Using this attribute should be avoided and used only as a last resort.

Bob
  • 13,447
  • 7
  • 35
  • 45
  • thanks for answering. If I don't want to restart the activity, I set `configChanges`. This means I will use the same resources, so I do not need to override the callback, right? – JackWM Aug 20 '17 at 19:34
  • Yes. That's the way the system informs you about the configuration change. It is up to you to decide what to do. It is totally fine not to override the callback if you don't want to change anything. – Bob Aug 20 '17 at 19:37
  • Bob, could you point out a typical use of the callback method? – JackWM Aug 20 '17 at 19:41
  • I haven't used it so far in any projects except to play with it. One usecase I can think of: when there is some performance limitation that you don't want the Activity to get recreated for every orientation change but requires only some views to get moved a bit. You can override the callback and check for the current orientation and move the views yourselves rather than recreating the whole Activity. – Bob Aug 20 '17 at 19:49
  • Good example, Bob. In this case, even restarting the activity won't help (Android does not know how you want to adjust some views), unless you design another layout, right? – JackWM Aug 20 '17 at 21:49
  • 1
    You have to adjust the Views manually in the `onConfugurationChanged()` callback, by checking the current orientation. – Bob Aug 21 '17 at 06:27