I am newbie and i working on an Android project using Hilt for dependency injection and the Navigation Component for navigation. I have one MainActivity as my host and two fragments: HomeFragment and DetailFragment.
The navigation flow works fine when I navigate from HomeFragment to DetailFragment. However, when I try to navigate back, the screen goes blank, and none of the fragments are displayed.
MainActivity
`@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
@Inject
lateinit var featuresNavigation: FeaturesNavigation
private val viewModel: MainViewModel by viewModels()
override fun getVM(): MainViewModel = viewModel
private lateinit var startDestination: Pair<Int, NavArgument?>
override val layoutId = R.layout.activity_main
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host) as NavHostFragment
featuresNavigation.bind(navHostFragment)
with(intent) {
initGraph(this)
data = null
replaceExtras(Bundle())
}
}
private fun initGraph(intent: Intent?) {
startDestination = createStartDestination(intent)
if (startDestination.first == R.id.homeFragment) {
Timber.e("MainAct test initGraph")
featuresNavigation.setGraph(
R.navigation.main_navigation,
startDestination.first,
startDestination.second
)
}
}
private fun createStartDestination(intent: Intent?): Pair<Int, NavArgument?> {
val bundle = intent?.getBundleExtra(Constants.KeyParam.KEY_EXTRAS)
return when (intent?.action) {
else -> {
R.id.homeFragment to null
}
}
}
}`
ActivityMain.xml
`<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<FrameLayout
android:id="@+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host"
android:name="com.usmh.libraries.navigationcomponent.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
tools:context=".ui.activity.MainActivity" />
</FrameLayout>
</layout>`
HomeFragment
`@AndroidEntryPoint
class HomeFragment :
BaseFragment<FragmentHomeBinding, HomeViewModel>(R.layout.fragment_home) {
@Inject
lateinit var featuresNavigation: FeaturesNavigation
private val viewModel: HomeViewModel by viewModels()
override fun getVM() = viewModel
override fun initView(savedInstanceState: Bundle?) {
super.initView(savedInstanceState)
activity?.window?.setBackgroundDrawable(null)
featuresNavigation.openDetail()
}
}`
DetailFragment
`@AndroidEntryPoint
class DetailFragment :
BaseFragment<FragmentDetailBinding, DetailViewModel>(R.layout.fragment_detail) {
@Inject
lateinit var featuresNavigation: FeaturesNavigation
private val viewModel: DetailViewModel by viewModels()
override fun getVM() = viewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// TODO: Use the ViewModel
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.fragment_detail, container, false)
}
override fun onBackPressed() {
featuresNavigation.navigateUp()
}
}`
AppNavigatorImpl
`@ActivityScoped
class AppNavigatorImpl @Inject constructor() : BaseNavigatorImpl(), FeaturesNavigation {
override fun openDetail(bundle: Bundle?) {
openScreen(R.id.action_homeFragment_to_detailFragment, bundle, R.id.detailFragment)
}
}`
FeaturesNavigation
interface FeaturesNavigation : BaseNavigator {
fun openDetail(bundle: Bundle? = null)
}
main_navigation.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/main_navigation"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.usmh.features.ui.HomeFragment"
android:label="HomeFragment"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/action_homeFragment_to_detailFragment"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/detailFragment"
android:name="com.usmh.features.ui.DetailFragment"
android:label="DetailFragment"
tools:layout="@layout/fragment_detail" />
</navigation>`
rocky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.