3

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

Community
  • 1
  • 1
Alkimake
  • 1,797
  • 14
  • 30

1 Answers1

2

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.

Sam Hogarth
  • 585
  • 4
  • 13
  • i tried everything including your suggestions. It works when i create a new layout in 'layout-land' folder. But my application will crash in 3 orientation changes because of OutOfMemoryException. If i put this background, it automatically gives memory-leak. And heap size increases %80-90 before crash up from %58. and memory increases 50 MB up from 23. – Alkimake Mar 03 '11 at 07:40
  • Upon changing the screen orientation, the current activity is restarted. If you keep any references to Context, you could be holding a reference to the destroyed Activity, or the background Drawable. Instead Context references should be tied to the lifecycle of the activity. Without seeing your actual code, this is my educated guess as to why there is a leak. The Android Documentation has some advice on avoiding memory leaks - I recommend ensuring your app follows these (http://developer.android.com/resources/articles/avoiding-memory-leaks.html) – Sam Hogarth Mar 03 '11 at 08:47
  • I ensure you my app follows the advices. I also asked question about memory leaks on SO. http://stackoverflow.com/questions/5116728/android-memory-usage-problem-on-possibly-using-activitygroup . As you see memory leaks just occurs when i put this background. Without background there is no oom problem and memory analyzer shows pretty stable application. It seems main reason of memory problem is this drawable. That is why i ended up with this result because i eliminated all memory issues and avoided them. – Alkimake Mar 03 '11 at 08:58
  • Did you try putting the background directly in a linerlayout tag???im having this problem too, and it seems its directly related to putting the background reference in the theme xml file. if i put it directly in the layout file it will work. of course this is not ideal because i would have to search every layout screen file and put the background reference in there. – Maxrunner Mar 08 '12 at 10:26