我在使用SearchView菜单项时发现内存泄漏。
这是我的代码:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (hasSearchOption()) {
MenuItem item = menu.findItem(R.id.action_search);
if (item == null) {
inflater.inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) Controller.getContext().getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
ComponentName cn = new ComponentName(getApplicationContext(), TestActivity.class);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(cn));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
//DO STUFF
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
}
}
类名
| Ref. Objects | Shallow Heap | Ref. Shallow Heap | Retained Heap
-----------------------------------------------------------------------------------------------------------------------------------------------------------
mOnQueryChangeListener com.test.fragments.TestFragmentImpl$1 @ 0xa58ff9c0| 1 | 16 | 16 | 16
-----------------------------------------------------------------------------------------------------------------------------------------------------------
如果我将设置setOnQueryTextListener的注释掉,泄漏就会消失。我不理解这种泄漏,因为搜索的主体是空的,那么它怎么会泄漏呢?