I am using leakcanary and it says your bottom sheet fragment is leaking. But i can't see where the problem is.
How can i fix that leak?
public class TokensExplainedFragment extends BottomSheetDialogFragment implements HasSupportFragmentInjector {
private static final String TAG = "TokensExplainedFragment";
private View mainView;
@Inject
DispatchingAndroidInjector<Fragment> childFragmentInjector;
@Inject
SessionManager sessionManager;
@Inject
ViewModelProviderFactory providerFactory;
public TokensExplainedFragment() {
}
@Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.fragment_tokens_explained, container, false);
return mainView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
return dialog;
}
@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
return childFragmentInjector;
}
}
This is how i start it:
TokensExplainedFragment bottomSheetFragment = new TokensExplainedFragment();
bottomSheetFragment.show(getActivity().getSupportFragmentManager(), bottomSheetFragment.getTag());
Leak trace:
According to the Android sources, ConnectivityThread is:
Shared singleton connectivity thread for the system. This is a thread for connectivity operations such as AsyncChannel connections to system services. Various connectivity manager objects can use this singleton as a common resource for their handlers instead of creating separate threads of their own.
Here we can see that this thread is running, and a message posted to it and currently executing on that thread has a reference to TokensExplainedFragment.
I suspect TokensExplainedFragment is doing more than what's shared in StackOverflow (e.g. some code using sessionManager), and in that extra code there might be something calling a connectivity system service (e.g. wifi, internet, bluetooth etc). That's likely triggering a delayed post of a message to the connectivity thread, and that post should be canceled but isn't.