Reputation: 836
I Have a empty dialog fragment. For get memory leak issue, I Add LeakCanary library to my app. When open dialog fragment with this commands:
DialogFragment fragment = TabsFragment.newInstance();
fragment.setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DialogFragments);
fragment.show(getSupportFragmentManager(), "MyFragment");
and close it, LeakCanary show me this Error:
I try and add setRetainInstance in OnCreate method and view = null in onDestroyView. But that memory leak error still showing.
This is my Fragment:
public class TabsFragment extends DialogFragment {
private View view;
public static TabsFragment newInstance() {
return new TabsFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
dismiss();
view = null;
}
}
How to fix this issue?
Upvotes: 3
Views: 1708
Reputation: 403
you can open dialog like this:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(TabsFragment.newInstance(), "Fragment");
ft.addToBackStack(null);
ft.commit();
And in onDismiss method in your dialog fragment, Write this codes:
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (fragmentManager.getBackStackEntryCount() > 0)
fragmentManager.popBackStack();
fragmentTransaction.commit();
Upvotes: 1