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"/>
Main Activity does not have a NavController
As it was described in the reference:
When creating the NavHostFragment using FragmentContainerView or if
manually adding the NavHostFragment to your activity via a
FragmentTransaction, attempting to retrieve the NavController in
onCreate() of an Activity via Navigation.findNavController(Activity,
@IdRes int) will fail. You should retrieve the NavController directly
from the NavHostFragment instead.
Looks like you should use
NavHostFragment navHostFragment =
(NavHostFragment) supportFragmentManager.findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();
instead of
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
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.
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);
Kotlin: does not have a NavController set
I had the same issue a while back. It works everywhere except in onCreate
. In onCreate
I had to find it like so:
val navigationHost =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navigationHost.navController
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
Related Topics
How to Change the Icon Color Selected on Bottom Navigation Bar in Android Studio
How to Set the Height of an Item Row in Gridlayoutmanager
How to Remove Focus from Edittext When User Is Done Editing
Android - How to Make a Button Click Play a Sound File Every Time It Been Pressed
How to Remove Bottom Navigation View and Toolbar in Some Fragments If Using Navigation Controller
How to Disable Cache in Android Webview
Android: How to Enable/Disable Option Menu Item on Button Click
Android Fastboot Devices Not Returning Device
Onactivityresult Is Not Being Called in Fragment
App Crashes (Sometimes) With Fatal Signal 11 (Sigsegv), Code 1
How to Validate Password Field in Android
Android No on Screen Keybord ( Only Voice )
Android.Support.V7.App.Activitycompat Not Found
Socket.Io Client Getting Connected Disconnected Repeatedly
How to Center Items of a Recyclerview
Background Drawable Not Applying to Button
Android Studio 0.4.3 - Task 'Assemble' Not Found in Root Project