Android Orientation change in tabactivity with fragment

197 Views Asked by At

I have implemented the tabactivity with 5 tabs. When i change orientation and tabchange i am getting the outOfMemory issue.

I tried in the way to

  1. setRetainInstance(true);

  2. Also set

    @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); }

in my fragment and framentactivity.

  1. Also added android:configChanges="keyboardHidden|orientation|screenLayout|screenSize" in manifest.

But no luck. I added my logcat below,

09-03 10:34:30.475: E/dalvikvm-heap(8691): Out of memory on a 6554896-byte allocation.
09-03 10:34:30.475: I/dalvikvm(25175): "main" prio=5 tid=1 RUNNABLE
09-03 10:34:30.475: I/dalvikvm(25175):   | group="main" sCount=0 dsCount=0 obj=0x41127508 self=0x410c5b78
09-03 10:34:30.475: I/dalvikvm(25175):   | sysTid=25175 nice=0 sched=0/0 cgrp=apps handle=1074904880
09-03 10:34:30.475: I/dalvikvm(25175):   | schedstat=( 7353555166 1878419412 11747 ) utm=577 stm=157 core=3
09-03 10:34:30.475: I/dalvikvm(25175):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.content.res.Resources.loadDrawable(Resources.java:1963)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.view.View.<init>(View.java:3444)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.view.View.<init>(View.java:3377)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.view.ViewGroup.<init>(ViewGroup.java:426)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.widget.RelativeLayout.<init>(RelativeLayout.java:184)
09-03 10:34:30.475: I/dalvikvm(25175):   at java.lang.reflect.Constructor.constructNative(Native Method)
09-03 10:34:30.475: I/dalvikvm(25175):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.view.LayoutInflater.createView(LayoutInflater.java:587)
09-03 10:34:30.475: I/dalvikvm(25175):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-03 10:34:30.480: I/dalvikvm(25175):   at com.dameco.Settings.onCreateView(Settings.java:113)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.Activity.performStart(Activity.java:5216)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.ActivityThread.startActivityNow(ActivityThread.java:1941)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:705)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.widget.TabHost.setCurrentTab(TabHost.java:369)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:560)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.View.performClick(View.java:4223)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.View$PerformClick.run(View.java:17275)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.os.Handler.handleCallback(Handler.java:615)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.os.Looper.loop(Looper.java:137)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.ActivityThread.main(ActivityThread.java:4898)
09-03 10:34:30.480: I/dalvikvm(25175):   at java.lang.reflect.Method.invokeNative(Native Method)
09-03 10:34:30.480: I/dalvikvm(25175):   at java.lang.reflect.Method.invoke(Method.java:511)
09-03 10:34:30.480: I/dalvikvm(25175):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
09-03 10:34:30.480: I/dalvikvm(25175):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
09-03 10:34:30.480: I/dalvikvm(25175):   at dalvik.system.NativeStart.main(Native Method)

Please advice me on this. Thanks.

2

There are 2 best solutions below

1
On

Remove this line because u already given in menifest related that

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } 
4
On

From the logs it seems that the view is trying to recreate a bitmap from a resource and fails due to OOM.

Bitmaps are not garbage collected immediately. The GC cycle in this case (Native Heap) runs with a bigger delay/interval. Try recycling the bitmap when changing orientation or try and retain the bitmap.

Not sure what you are doing, posting some more code would definitely help.

Also don't use configChanges it is a bad practice. Specifying this tells the activity manager that you will take care of everything related to config change and the system will not do anything for you.

From an SO answer by S.D.

setRetainInstance(true): It is recommended to be used with fragments that do not hold any reference to anything, that will be recreated on rotation. This means you should not use it on any Fragment that holds Context, Views, etc. A typical Visual fragment does. But it is very useful to with Fragments that hold objects like running Threads, AsyncTasks, Data Collections, loaded assets, fetched results etc. This method help using a non visual Fragment, as a detachable holder, for non Context-dependent objects of an Activity.