I have detected a small memeory leak with leakCanary
in my app that I can't fix.
The leak gets detected when I open my NavigationDrawer
and opens a DialogView
once and then close it and then open my navdrawer again a couple of times. EDIT: Opening the dialog and dismissing it once and just leaving the drawer open will also leak
This is the stacktrace of the leak:
NavDrawerFragment.class: in the fragmetns onDetach() I set the references to null but it dosen't help either
@OnClick(R.id.navDrawer_newList_btn)
public void onNewListBtnClick() {
addListDialog = new AddShoplistDialog();
addListDialog.show(getFragmentManager(), AddShoplistDialog.FRAGMENT_TAG);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
selectionListener = (NavDrawerContract.OnSelectionListener) activity;
}
@Override
public void onDetach() {
super.onDetach();
//These dont't solve the leak
selectionListener = null;
addListDialog = null;
recyclerAdapter = null;
}
NavDrawerListAdapter.class
This is the RecyclerViewAdapter
for the RecyclerView
in the NavigationDrawer
public NavDrawerListAdapter(@NonNull Context context, @Nullable OrderedRealmCollection<Shoplist> data, NavDrawerContract.OnListItemActions selectionListener) {
super(context, data, true);
this.realm = Realm.getDefaultInstance();
this.selectionListener = selectionListener;
}
//Called when NavDrawerFragment's onDestroyView() is called
public void onCloseRealm() {
realm.close();
selectionListener = null;
}
ViewHolder in the NavDrawerListAdapter.Class
selectionListener
is also used here in the adapters ViewHolder
public class Viewholder extends RecyclerView.ViewHolder implements PopupMenu.OnMenuItemClickListener, View.OnClickListener {
public Viewholder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
selectionListener.onSelectedList(getItem(getAdapterPosition()).getId(), true);
selecetedPos = getAdapterPosition();
notifyDataSetChanged();
}