How to minimize activity leaks on Android

I am loading a lot of bitmaps in every activity. After switching between activities, I was getting a lot of "java.lang.OutOfMemoryError". My app is mainly focused on rubbing.

I tried many solutions and found a relatively better approach in one of the solutions on this website.

First, set the "id" attribute on the parent view of your XML layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/RootView"
 >

      

Then, in the onDestroy () method of your activity, call the unbindDrawables () method passing the refence to the parent view, and then execute the System.gc () command

@Override
    protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.RootView));
    System.gc();
    }

    private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
        ((ViewGroup) view).removeAllViews();
        }
    }

      

But it doesn't completely remove java.lang.OutOfMemoryError

. Now I am accidentally getting this memory exception. I read several posts that to get rid of this problem completely, you will have to load bitmaps from code and release them in an onDestroy action, which is not a practical solution in my case because I am loading a lot of bitmaps.

Does anyone have a better solution to this problem?

thank

+3


source to share





All Articles