Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android activity orientation change , each time new instance

I have created very simple activity TempActivity with no contentview, just for testing purpose. When this activity loads into android phone, I did around 40 times orientation change, I took heap file as hprof and exported to MAT tool and inspected. It shows 40 instance of my TempActivity, and in each instance many classes for actionbar view like linearlayout, imageview etc.

Each time whenever I rotate screen, DDMS shows more and more memory added to heap for this application, ideally shouldn't it be garbage collected after some time? When I forcefully did GC, still it didnt get cleared and it shows increased memory in heap.

Please let me know that is this the default behavior of Android, can we do something to remove all previous instances of an activity? Is this the case of memory leak? Because in my realtime application (with images and other stuff in contentview), I face the same issue when i rotate screen , each time new instance activity is created and heap size keeps increasing.

Is it not because of default ActionBar imageview, linearlayout or other controls automatically inbuilt each android activity right?

Here is my temp activity class:

[Activity(Label = "TempAcivity", MainLauncher = true)]
public class TempAcivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Create your application here
    }
}

EDIT

Here is my manifest file and style file which is applied for application

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="HF.Mobility.Android" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="21" />
<application android:label="Health Hub" android:theme="@style/CustomHoloTheme" android:icon="@drawable/ApplicationIcon" android:allowBackup="false">
    <application android:label="@string/ApplicationName"></application>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>

Style file:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
 <style name="CustomHoloTheme" parent="@android:style/Theme.Holo.Light">
  <item name="android:windowContentOverlay">@null</item>
  <item name="android:windowActionBarOverlay">true</item>
  <item name="android:icon">@android:color/transparent</item>
  <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance </item>
  <item name="android:typeface">monospace</item>
 </style>

 <style name="MyTheme.ActionBarStyle"   parent="@android:style/Widget.Holo.Light.ActionBar">
  <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  <item name="android:background">@drawable/actionbar_background</item>
 </style>
 <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
  <item name="android:textColor">#FFFFFF</item>
 </style>
 <style name="myCustomMenuTextApearance" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
  <item name="android:textColor">@color/xam_green</item>
 </style> 
</resources>

Here is actionbar_background.xml file :

<?xml version="1.0" encoding="utf-8" ?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
   <shape>
     <solid android:color="#00000000" />
   </shape>
  </item>
</layer-list>
like image 213
Suketu Avatar asked Oct 31 '22 08:10

Suketu


1 Answers

Edit: Doing what @Matt R outlines in his linked post seems like the best way to go, and my answer below is more of a "last resort", which is how the Android documentation classifies it as well.

From the documentation:

Note: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

Original post:

It sounds like adding this to your AndroidManifest.xml would help, as it would make it so that the Activity is not re-created on orientation change:

<activity android:name=".TempAcivity"
          android:configChanges="orientation|screenSize|keyboardHidden"
          android:label="@string/app_name">

Documentation states of this configuration:

Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.

Link: http://developer.android.com/guide/topics/resources/runtime-changes.html

See this post as well: How to detect orientation change in layout in Android?

like image 189
Daniel Nugent Avatar answered Nov 11 '22 10:11

Daniel Nugent