I start an activity B from activity A. And want activity A to be completely destroyed and all the memory it is holding to be released, so that activity B starts as if it is starting first. I have a button onClick method, which finishes this activity and uses the method unbindDrawables copied from here.
//inside activity A
public void onClickMethod(View v){
Intent intent = new Intent(this, EarthRoomActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);// starting activity B
unbindDrawables(findViewById(R.id.water_room_flipper));
this.finish();
}
static void unbindDrawables(View view) {
try{
System.out.println("UNBINDING"+view);
if (view.getBackground() != null) {
((BitmapDrawable)view.getBackground()).getBitmap().recycle();
view.getBackground().setCallback(null);
view=null;
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}}
But nevertheles I'm getting this error:
10-28 20:20:14 .220: E/AndroidRuntime(22709): FATAL EXCEPTION: main
10-28 20:20:14.220: E/AndroidRuntime(22709):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.room/com.example.room.EarthRoomActivity}:
android.view.InflateException: Binary XML file line #1: Error
inflating class <unknown> 10-28 20:20:14.220: E/AndroidRuntime(22709):
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.ActivityThread.access$600(ActivityThread.java:128) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.os.Handler.dispatchMessage(Handler.java:99) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.os.Looper.loop(Looper.java:137) 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
android.app.ActivityThread.main(ActivityThread.java:4514) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
java.lang.reflect.Method.invokeNative(Native Method) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
java.lang.reflect.Method.invoke(Method.java:511) 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
dalvik.system.NativeStart.main(Native Method) 10-28 20:20:14.220:
E/AndroidRuntime(22709): Caused by: android.view.InflateException:
Binary XML file line #1: Error inflating class <unknown> 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.createView(LayoutInflater.java:606) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.parseInclude(LayoutInflater.java:800)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:729) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.inflate(LayoutInflater.java:489) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.inflate(LayoutInflater.java:352) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:280)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.Activity.setContentView(Activity.java:1892) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
com.example.room.EarthRoomActivity.onCreate(EarthRoomActivity.java:28)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.Activity.performCreate(Activity.java:4562) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-28 20:20:14.220: E/AndroidRuntime(22709): ... 11 more 10-28
20:20:14.220: E/AndroidRuntime(22709): Caused by:
java.lang.reflect.InvocationTargetException 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
java.lang.reflect.Constructor.constructNative(Native Method) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
java.lang.reflect.Constructor.newInstance(Constructor.java:417) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.createView(LayoutInflater.java:586) 10-28
20:20:14.220: E/AndroidRuntime(22709): ... 26 more 10-28
20:20:14.220: E/AndroidRuntime(22709): Caused by:
java.lang.OutOfMemoryError 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.content.res.Resources.loadDrawable(Resources.java:1968) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.content.res.TypedArray.getDrawable(TypedArray.java:601) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.View.<init>(View.java:2857) 10-28 20:20:14.220:
E/AndroidRuntime(22709): at android.view.View.<init>(View.java:2794)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.ViewGroup.<init>(ViewGroup.java:381) 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
android.widget.RelativeLayout.<init>(RelativeLayout.java:174) 10-28
20:20:14.220: E/AndroidRuntime(22709): ... 29 more
I have to mention that when I start activity B from android manifest making it the first app to be launched, it launches without errors.
Activity B xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="fill_parent" android:id="@+id/earth_room_layout"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ViewFlipper android:id="@+id/water_room_flipper"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<include layout="@layout/earth_room_wall1" android:id="@+id/first" />
<include layout="@layout/earth_room_wall2" android:id="@+id/second" />
<include layout="@layout/earth_room_wall3" android:id="@+id/third" />
<include layout="@layout/earth_room_wall4" android:id="@+id/fourth" />
</ViewFlipper>
<com.example.room.CustomView android:id="@+id/customView"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>
If I remove last from the ViewFlipper it works. I.e. it has to do smth with OutOfMemory error. But I did release everything activity A was holding, didn't I? Or may be I missed smth?