I'm trying to use viewmodel and observe the data in my DialogFragment, I'm Injecting ViewModel as well, while implementing the code getting error. PFB my code snippet. And help me out with suitable solution.
- Current DialogFragment
@AndroidEntryPoint
public class MyFragmentFragment extends DialogFragment implements View.OnClickListener {
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_buycourse, container, false);
myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
view = mBinding.getRoot();
initViews();
return view;
}
public static MyFragmentFragment newInstance(){
return new MyFragmentFragment ();
}
}
- Parent Fragment in code
@AndroidEntryPoint
public class HomeFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mBinding.startBtn.setOnClickListener(v -> {
DialogFragment dialog = new MyFragmentFragment ();
dialog.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Theme_Learn365_NoActionBar);
dialog.show(requireActivity().getFragmentManager(), "MyFragment");
}
}
}
When I run this, I'm getting "@AndroidEntryPoint base class must extend ComponentActivity, (support) Fragment, View, Service, or BroadcastReceiver." And I'm not able to Inject ViewModel in DialogFragment.