0

I have a TestActivity. This activity will be started from a Main activity upon a Button press. This is how my Test Activity is declared in the manifest file.

  <activity
        android:name="com.example.MyTestActivity"
        android:configChanges="keyboard|keyboardHidden"
        android:label="MYTESTACtivity"
        android:theme="@style/someTheme"
        android:windowSoftInputMode="stateHidden|adjustPan" >
    </activity>

Now the problem is, once the TestActivity screen is shown, if I do an orientation change, then the instance count of TestActivity is increasing by 1. If I again change it to Portrait mode, it is increasing by 1 more. This is how the instance count keeps on raising during Orientation Changes.

Please advice how to make just one activity instance at a time no matter what the orientation changes are. Also I have tried adding android:launchMode="SingleTop" still same issue is present. FYI, This is how I am able to find the instance count with strict mode.

01-02 01:39:48.855: E/StrictMode(21992): android.os.StrictMode$InstanceCountViolation: class com.example.MyTestActivity; instances=20; limit=1
Akshay
  • 2,506
  • 4
  • 34
  • 55
Guna
  • 338
  • 6
  • 17
  • try with android:launchMode="singleInstance" – Ajay Oct 30 '12 at 06:07
  • android:singleInstance is not recommended to use, hence can not use it as well. – Guna Oct 30 '12 at 06:15
  • Do you have a context leak? It is possible that you are holding on to a reference to something in your activity somewhere else in the application which is preventing the previous instances of the activity being garbage collected. – Simon Oct 30 '12 at 06:58
  • I dont think so, I have 2 different layouts in layout and layout-land-xhdpi folders, I did not declare my activity with android:configChanges either. So onCreate() getting called again and again with orientation changes, and my activity instance count is getting increased. Also I cannot declare android:configChanges because, then I will not able to use onSaveInstanceState and onRestoreInstanceState as well. – Guna Oct 30 '12 at 11:57
  • The onCreate() call is expected. Each time you change the orientation, your activity is destroyed then recreated which causes onCreate() to be called. However, it seems that the existing instances are not being destroyed and the only thing I know of that causes that is a leak. Is it possible to post your activity code? – Simon Oct 30 '12 at 11:59
  • Hi Simon, thanks, I see some bitmap leak. Actually I am creating a bitmap, binding it to drawable and to an ImageView. Like this am creating some number of ImageVIews finally adding them to a GridLayout, to scroll it correctly am adding it to a HorizontalScrollView. I am using MAT. Still could not pinpoint to the exact location of mem leak. – Guna Oct 31 '12 at 05:54
  • Simon Pls post your comment as an answer, I will accept it, that indeed was a memory leak, hence many instances were getting created. – Guna Oct 31 '12 at 11:29
  • I'm pleased that you found it. Cheers – Simon Oct 31 '12 at 12:02

4 Answers4

0

I decide to move my comment to an answer, and expand it, in the hope that other newcomers finding this post will not accept this as a solution without further thought:

android:configChanges:orientation|screensize

Android is designed to destroy and recreate your activity on configuration changes for a very good reason. What you are doing by adding to this to your manifest is saying "I understand what I am doing. I don't want Android to behave in the way it was designed and I will handle all of the consequences.

In some circumstances, this is the right solution but for most situations, it is the wrong one and far from "my problem is solved", in fact your problem is probably just beginning as you have to write more code to do what Android would do automatically. More code = more bugs = lower maintainability.

The OP does not give enough detail to provide a precise answer but, a generically correct answer is to learn about the Activity lifecycle (i.e. how Android is designed to work) and think about how it applies to your app and how to handle the various callbacks. In particular, understanding the differences between onCreate(), onStart() and onResume() and between onPause() and onStop() is key. For example, putting lots of code in onCreate() is a common beginners mistake. I've seen apps with almost all of their significant code done in onCreate() and it's not pretty. The developer presses the home button for the first time, then returns to their app and wonders why it doesn't work correctly.

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Yddl gives a nice explanation here:

Why not use always android:configChanges="keyboardHidden|orientation"?

If you want to upvote, then upvote him as all I have done is reference his response.

Community
  • 1
  • 1
Simon
  • 14,407
  • 8
  • 46
  • 61
0

The onCreate() call is expected. Each time you change the orientation, your activity is destroyed then recreated which causes onCreate() to be called. However, it seems that the existing instances are not being destroyed and the only thing I know of that causes that is a leak. A common cause is to not unbind bitmaps when the activity is destroyed.

Simon
  • 14,407
  • 8
  • 46
  • 61
  • Also to add to Simon's answer,I think there is a problem with Strict Mode code. I have called System.gc() in onDestroy() of my Activity and re-run the same use case,now the max count its getting to was 6 or so,and if I give a little time the activity count shown in strict mode log is even falling down to 2,I know we need not call System.gc() from our activity,but just tried to see if this really was a memory leak in my activity.So its like Strict mode is showing weaker references to an activity,but once Garbage Collector job is done then strict mode shows the actual activity count correctly. – Guna Nov 05 '12 at 08:59
-2

By default, when the screen orientation changes at runtime (the user has rotated the device),the activity is shut down and restarted. To prevent this add following to your manifest.

android:configChanges="keyboardHidden|orientation"

So your manifest will looks like follows.

<activity
    android:name="com.example.MyTestActivity"
    android:configChanges="keyboard|keyboardHidden"
    android:label="MYTESTACtivity"
    android:theme="@style/someTheme"
    android:windowSoftInputMode="stateHidden|adjustPan" 
    android:configChanges="keyboardHidden|orientation">
</activity>

For more info check this

Community
  • 1
  • 1
saji159
  • 328
  • 7
  • 14
  • Make sure that you read that link. Many people think android:configChanges="keyboardHidden|orientation" is a solution whilst not understanding what it does. Learning the Activity lifecycle, http://developer.android.com/guide/components/activities.html, then thinking about how your app needs to deal with it is the correct approach. – Simon Oct 30 '12 at 06:07
  • I have a seperate Layout changes which needs to be displayed during landscape mode, hence I cannot put this android:configChanges:orientation|screensize keyword in my app. Thanks – Guna Oct 30 '12 at 06:09
-3

Try adding this:

<activity
    android:name="com.example.MyTestActivity"
    android:configChanges="keyboard|keyboardHidden|orientation"
   ...../>

Your activity will not get restarted during the orientation chenges.

AndroGeek
  • 1,280
  • 3
  • 13
  • 25
  • This is exactly what you don't want to do when you find 20 instances still running. An obvious leak is present and it should be dealt with. Not dealing with it will ensure that your application is using more resources than necessary when not currently in use and that is a VERY BAD thing for the end user. – JRomero May 27 '13 at 20:22