I have 3 fragments Easy , Medium and Hard, the defualt one is Easy, so, I am using Navigation graph to navigate to respective fragments when clicked on the Tabs, for example if I click on Medium Tab, I should navigate to Medium Fragment. So, now when I run the code, it's showing following error
Below is my code. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- android:numColumns=2 is the number of columns for Grid View
android:horizontalSpacing is the space between horizontal
grid items -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Below is the code of my MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.Navigation
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize the NavController
navController = Navigation.findNavController(this,
R.id.nav_host_fragment)
}
// Ensure that the back button works with the NavController
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
Below is the code of my EasyFragment, code is same for other 2 fragments also
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
class EasyFragment : Fragment() {
private lateinit var viewPager: ViewPager2
private lateinit var tabLayout: TabLayout
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_easy, container, false)
// Initialize the ViewPager2 and TabLayout
viewPager = view.findViewById(R.id.viewPager)
tabLayout = view.findViewById(R.id.tabLayout)
// Create an array of fragment instances for each tab
val fragments = listOf(
EasyFragment(), // You should create separate fragment
classes for Easy, Medium, and Hard tabs
MediumFragment(),
HardFragment()
)
val adapter = ViewPagerAdapter(requireActivity(), fragments)
viewPager.adapter = adapter
return view
}
}
Below is the Adapter class code
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
class ViewPagerAdapter(
fragmentActivity: FragmentActivity,
private val fragments: List<Fragment>
):FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int {
return fragments.size
}
override fun createFragment(position: Int): Fragment {
when (position) {
0-> return EasyFragment()
1-> return MediumFragment()
2-> return HardFragment()
}
return EasyFragment()
}
Below is the Navigation Graph code nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/easyFragment">
<fragment
android:id="@+id/easyFragment"
android:name="com.example.tablayoutdemo.EasyFragment"
android:label="fragment_easy"
tools:layout="@layout/fragment_easy" >
<action
android:id="@+id/action_easyFragment_to_mediumFragment"
app:destination="@id/mediumFragment" />
<action
android:id="@+id/action_easyFragment_to_hardFragment"
app:destination="@id/hardFragment" />
<action
android:id="@+id/action_easyFragment_to_easyFragment"
app:destination="@+id/easyFragment"/>
</fragment>
<fragment
android:id="@+id/mediumFragment"
android:name="com.example.tablayoutdemo.MediumFragment"
android:label="fragment_medium"
tools:layout="@layout/fragment_medium" />
<fragment
android:id="@+id/hardFragment"
android:name="com.example.tablayoutdemo.HardFragment"
android:label="fragment_hard"
tools:layout="@layout/fragment_hard" />
</navigation>
It says on your error code that activity does not have a nav controller set.
this caused because of improper initialization.
try this instead, Use NavHostFragment
.
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController