使用InstantSearch-Android时会出现MemoryLeak

时间:2017-10-23 13:34:12

标签: android algolia instantsearch instantsearch-android

当我使用InstantSearch Android时,会出现内存泄漏。

请参阅以下MainActivity泄漏内存... hprof表明com.algolia.instantsearch.ui.views.Hits存在问题。当Activity被摧毁时,有人知道如何销毁该视图吗?

public class MainActivity extends AppCompatActivity {
    private Searcher searcher;
    InstantSearch helper;

    Hits hits;

    LinearLayout activity_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hits= (Hits) findViewById(R.id.hits);
        activity_main= (LinearLayout) findViewById(R.id.activity_main);

        searcher = Searcher.create("app_id","secret_key","Items");
        helper = new InstantSearch(this, searcher);
        helper.search();
    }

    @Override
    protected void onDestroy() {
        searcher=null;

        helper=null;
        hits.clear();

        hits.removeAllViewsInLayout();
        hits.removeAllViews();
        hits.destroyDrawingCache();
        hits.setBackground(null);
        hits.setBackgroundResource(0);
        hits=null;

        activity_main.removeAllViewsInLayout();
        activity_main.removeAllViews();
        activity_main.destroyDrawingCache();
        activity_main.setBackground(null);
        activity_main.setBackgroundResource(0);
        activity_main=null;
        Log.i("AppInfo","Destroy");
        super.onDestroy();
    }
}

这是我的activity_main.xml

<LinearLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
xmlns:algolia="http://schemas.android.com/apk/res-auto">


<com.algolia.instantsearch.ui.views.Hits
    android:id="@+id/hits"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    algolia:itemLayout="@layout/hits_item"
    algolia:autoHideKeyboard="true"
    algolia:hitsPerPage="20"/>

这是我的hits_item.xml

<layout xmlns:algolia="http://schemas.android.com/apk/res-auto">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/profImg"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:src="@drawable/placeholder_video"
            algolia:attribute='@{"profilePic"}'/>

    <TextView
        android:id="@+id/hit_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        algolia:attribute='@{"name"}'
        algolia:highlighted='@{true}'/>

 </RelativeLayout>

1 个答案:

答案 0 :(得分:8)

我们刚刚发布修复程序。如果您更新到1.1.4,现在可以解决此问题。只需添加您的活动onDestroy以下代码:

@Override
protected void onDestroy() {
    // ... your code ...
    searcher.destroy(); // This will clean and release different listeners 
    super.onDestroy();
}

你可以看到它here