Navigation Component, does not have a NavController set , does not have a NavController set at androidx.navigation.Navigation.findNavController

Viewed 8124

I am basically trying to navigate to other fragment after waiting 5 seconds.When I do the navigation process in my onViewCreated it works but when i do it in onFinish it gives me the error below.

I tried to keep View object as a global variable but it didn't work either.

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ui, PID: 17838 java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{8dcd038 V.E...... .......D 0,0-1080,1908} does not have a NavController set at androidx.navigation.Navigation.findNavController(Navigation.java:84) at com.example.ui.sign_in.LoadingFragment$1.onFinish(LoadingFragment.java:39) at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6810) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) I/Process: Sending signal. PID: 17838 SIG: 9

** private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_loading, container, false);
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    this.view = view;

    new CountDownTimer(5000, 1000) { //Wait 5 seconds
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() { // Then navigate to other page.
            navigate();
        }
    }.start();

    super.onViewCreated(view, savedInstanceState);
}


private void navigate() { 
  Navigation.findNavController(this.view).navigate(LoadingFragmentDirections.actionLoadingFragmentToSignInPagerFragment());
}**
4 Answers

Two ways to get the Nav Controller from a fragment:

  1. NavHostFragment.findNavController(this);

  2. Navigation.findNavController(requireActivity(), R.id.MAIN_NAV_HOST_FRAGMENT);

Solution 1)

You need to check if fragment is visible or not anymore before onFinish() get called

Add a boolean , initialize it in onResume() and make it false in onPause() like this

private boolean isFragmentVisible=false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_loading, container, false);
}



@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    this.view = view;

    new CountDownTimer(5000, 1000) { //Wait 5 seconds
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() { // Then navigate to other page.

           //check if fragment is visible or not
        if(isFragmentVisible){
          navigate();
        }
            
        }
    }.start();

    super.onViewCreated(view, savedInstanceState);
}

  @Override
    public void onResume() {
        super.onResume();
         isFragmentVisible=true;
    }

    @Override
    public void onPause() {
        super.onPause();
        isFragmentVisible=false;
    }
private void navigate() { 
  Navigation.findNavController(this.view).navigate(LoadingFragmentDirections.actionLoadingFragmentToSignInPagerFragment());
}**

Solution 2) You need to declare an instance of CountDownTimer and initialize it once and make it null when fragment is no more visible

I am not sure if this works for you, but i had such similar instance of issue, where i noticed the view object was being destroyed. So it preserve it my own way :)

This variable is declared under member variables of MyExampleFragment class

 private View mFragView;

Inside the onCreateView, i am just adding a null check, so it does not get created again.

 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (mFragView != null) {
        return mFragView;
    }
    mFragView = inflater.inflate(R.layout.fragment_dashboard, container, false);
    ButterKnife.bind(this, mFragView);
   
    return mFragView;
}

i change my code:

val action =
        StartFragmentDirections.actionStartFragmentToLoginFragment()
    Navigation.findNavController(view).navigate(action);

to onViewCreated

Related