In android background image does not change when orientation does

4k views Asked by At

I have a linear layout for my ActivityGroup as defined

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:mtx="http://schemas.android.com/apk/res/com.matriksdata"
  android:id="@+id/homeActivityGroupBG"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="@drawable/background">

And i have 2 background drawables located in "drawable" and "drawable-land" folders. When orientation changes everything is normal but background is not changed according to orientation. It always keeps the very first drawable for background.

I tried to change it manually in onConfigurationChanged method by adding the line:

background.setBackgroundResource(R.drawable.background);

it solves the problem. But it causes huge amount of memory leaks every time configuration changes or passing through activities.

Edit: I create a theme.xml to define background image to window. XML file contains:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="android:windowBackground">@drawable/background</item>
</style>
</resources>

I changed AndroidManifest.xml as

<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true" android:name="com.matriksdata.app.DefaultApplication"
android:theme="@style/Theme">

and i removed background elements from my layouts. Simply nothing has changed. I can not get new drawable when device orientation changes and i get memory-leaks that causes application crashes eventually.

Is there any other way to force to change drawables when orientation does? Or is there any reason that the application makes memory-leaks ?

Edit : for memory leak problem i had asked a question at Android Memory Usage Problem on possibly using ActivityGroup

1

There are 1 answers

4
Sam Hogarth On BEST ANSWER

Rather than switch the drawable upon orientation change, you can instead switch the layout. By placing an xml file of the same name in the layout-land/ folder, the OS will load this alternate layout when the screen orientation is landscape. That way, you will always be using the correct drawable (as they can be specified differently in each of the two xml files), and there is the added advantage that you can independently optimize your layout for both orientations!

This post on the Android Developer's blog suggests some tips and tricks on how to retain objects upon orientation change and avoid memory leaks in the process; which will be of further assistance to you.