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
android android-layout


source to share


No one has answered this question yet

See similar questions:

1
Code notation in Android

or similar:

3606
Close / hide Android soft keyboard
3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
3288
Correct use cases for Android UserManager.isUserAGoat ()?
2735
Stop EditText from getting focus when Activity starts
2609
Is there a unique identifier for an Android device?
2510
How to persist android activity state by persisting instance state?
2097
Is there a way to run Python on Android?
1296
Reloading activity on android rotation
1152
Difference between gravity and layout_gravity on Android
1042
The result was a leak of the window that was originally added



All Articles
Loading...
X
Show
Funny
Dev
Pics