How to destroy/release resources used in 1 activity/layout?

我们两清 提交于 2019-12-31 05:07:28

问题


How do I release resources used in 1 activity? So I got 3 layouts and activity for each layout, but the problem is when I switch between those activities my app crashes and I get this:

I started getting this error ever since I added adds to my application. My whole logcat http://i.imgur.com/t5G94dC.png

XML Layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000000" >

      <ImageButton
          android:id="@+id/dugme2"
          android:layout_width="150dp"
          android:layout_height="90dp"
          android:layout_alignBottom="@+id/dugme1"
          android:layout_alignParentRight="true"
          android:background="#0000"
          android:onClick="onClick"
          android:scaleType="fitXY"
          android:src="@drawable/dragunov" />

      <ImageButton
          android:id="@+id/dugme5"
          android:layout_width="150dp"
          android:layout_height="90dp"
          android:layout_alignParentLeft="true"
          android:layout_alignTop="@+id/dugme6"
          android:background="#0000"
          android:onClick="onClick"
          android:scaleType="fitXY"
          android:src="@drawable/scout" />


      <SeekBar
          android:id="@+id/seekSnipers"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:progressDrawable="@drawable/red_scrubber_progress"
          android:thumb="@drawable/red_scrubber_control" />

    <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            ads:adSize="BANNER"
            ads:adUnitId="ID"
            ads:loadAdOnCreate="true" >
            </com.google.ads.AdView>

</RelativeLayout>

It's pretty much same for other layouts as well, except for different resources and ID's. Ad code is same in every layout.


回答1:


YOu can release resources by using the following code. This should be called in onDestroy() of u=your activity.

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();
        view.setBackgroundResource(0);
    }
}

More over to destroy activity you should call finish() after calling next activity.




回答2:


You should use OnStop() callback to release the resources that causing the memory leak. You should also use onStop() to perform relatively CPU-intensive shutdown operations.



来源:https://stackoverflow.com/questions/21328745/how-to-destroy-release-resources-used-in-1-activity-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!