Illegalstateexception: Link Does Not Have a Navcontroller Set

IllegalStateException: Link does not have a NavController set

UPDATED SOLUTION

Actually, Navigation can't find NavController in FrameLayout. So replacing <FrameLayout> with <fragment> will make it work.

Add the following inside the <fragment> tag -

android:name="androidx.navigation.fragment.NavHostFragment"

After doing the changes, the code will look similar to this -

 <fragment
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/main_navigation"
app:defaultNavHost="true"/>

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

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

Activity does not have a NavController set on

You can get the Nav Controller using Java like this:

final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
final NavController navController = navHostFragment.getNavController();

And then you setup your bottom navigation like this:

NavigationUI.setupWithNavController(bottomNavigationView, navController);

findNavController() in Fragment gives does not have a NavController set error

I've downloaded an example from Github that helped me fix this problem.

According to the example this code worked for me:

class DashboardActivity : BaseActivity() {

private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)

val navHostFragment = supportFragmentManager.findFragmentById(
R.id.dashboardNavHostFragment
) as NavHostFragment
navController = navHostFragment.navController

// Setup the bottom navigation view with navController
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
bottomNavigationView.setupWithNavController(navController)

// Setup the ActionBar with navController and 2 top level destinations
appBarConfiguration = AppBarConfiguration(
setOf(R.id.scanFragment, R.id.profileFragment)
)
setupActionBarWithNavController(navController, appBarConfiguration)
}

override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration)
}

}

How do I fix error java.lang.IllegalStateException: Activity does not have a NavController...?

--------- Answer -----------

Follow the steps below:

    private val navHostFragment =  supportFragmentManager
.findFragmentById(R.id.activity_main_nav_host_fragment) as NavHostFragment

navController = navHostFragment.navController

After this you can use navcontroller.

Feel free to ask if something is unclear.



Related Topics



Leave a reply



Submit