Search code examples
androidandroid-actionbarandroid-actionbar-compat

Redraw ActionBar in Android manually


I have an Android app and layout is available in both landscape and portrait.

On some activities there are a lot of inputs and selections and I want to keep them even if I rotate the phone.

I added this to the Activity in my manifest:

android:configChanges="orientation|screenSize|keyboardHidden|keyboard"

Inputs are kept when rotating from portrait to landscape and everything looks good, except the size/textsize of my ActionBar. It does not change. If I start an Activity in landscape mode, the text size is smaller but of course does not become bigger when rotating back to portrait mode.

Is there a way, to redraw the ActionBar (like in onConfigurationChanged() or something) or to easily keep all my inputs.


Solution

  • Using configChanges in this case is really bad practice.

    Yes, it will preserve your inputs and states on an orientation change, but your Activity can be recreated because of other reasons, for example when your app is in the background and the OS decides to kill it to reclaim memory and then you navigate back to it.

    When you reopen your app, the OS will try to recreate the Activity as you left it, but will fail to do so (most probably resulting in a crash).

    Instead of using the configChanges attribute, you should save your stuff in onSaveInstanceState() and restore it from the Bundle in onCreate().

    Check out the developer's guide on this topic.