7

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.

  1. 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 ();
    }
}
  1. 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.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
Amritesh Kumar
  • 207
  • 1
  • 5
  • 16

1 Answers1

0

I resolved this issue by replacing the DialogFragment import and its working fine;

Old Import: import android.app.DialogFragment;

New Import: import androidx.fragment.app.DialogFragment;

Amritesh Kumar
  • 207
  • 1
  • 5
  • 16
  • 4
    I am still having this issue. All of my imports are androidx – petestmart Jul 08 '22 at 15:39
  • 1
    @petestmart Did you manage to fix the problem? I'm having the same issue with Activities, and in the end it's using import androidx.appcompat.app.AppCompatActivity, which should be the right one. – latsson Sep 01 '22 at 09:55
  • 1
    @latsson unfortunately, no. I had to abandon the Hilt migration project I was working on for something more urgent and never came back to it. – petestmart Sep 02 '22 at 17:23
  • 1
    @petestmart I managed to resolve my issue, but sadly now I don't remember what I did :/ – latsson Sep 05 '22 at 11:55
  • @latsson modify activity file in any way – k4dima May 01 '23 at 20:53