class Fragment extends Fragment() implements Callback {
@Ovverride
onCreate(SavedInstanceState savedInstanceState) {
Adapter adapter = new Adapter(getContext())
}
@Ovverride
someCallbackMethod() { // Code when callback is called }
}
class Adapter extends RecyclerView.Adapter<VH>() {
Context context;
public Adapter(Context context) {this.context = context}
class VH extends RecyclerView.ViewHolder {
private void bind() {
button.setOnClickListener (v-> {
BottomSheet sheet = BottomSheet.newInstance(variable);
sheet.show(((FragmentActivity) context).getSupportFragmentManager(),
sheet.getClass().getName());
})
}
}
class BottomSheet extends BottomSheetDialogFragment {
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof Callback) listener =
(Callback) context;
else Log.v("BottomSheet", "Not instance");
}
}
Even though the main fragment implements the callback the context of that fragment is never the instance of the callback what might be the reason for this and how can it be fixed. Note: There might be some errors here and there because I made a generic view of the classes.