使用Hilt与Navigation Component时返回导航出现空白屏问题
Android导航返回空白问题排查与解决
问题描述
我是一名Android开发新手,正在开发一个使用Hilt进行依赖注入、Navigation Component实现导航的项目。项目中有一个作为宿主的MainActivity,以及HomeFragment和DetailFragment两个碎片。从HomeFragment导航到DetailFragment的流程正常,但尝试返回或向上导航时,屏幕变为空白,没有任何碎片显示。
相关代码
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>
NavHostFragment
public open class NavHostFragment : Fragment(), NavHost { private var navHostController: NavHostController? = null private var isPrimaryBeforeOnCreate: Boolean? = null private var viewParent: View? = null // State that will be saved and restored private var graphId = 0 private var defaultNavHost = false final override val navController: NavController get() { checkNotNull(navHostController) { "NavController is not available before onCreate()" } return navHostController as NavHostController } @CallSuper public override fun onAttach(context: Context) { super.onAttach(context) if (defaultNavHost) { parentFragmentManager.beginTransaction() .setPrimaryNavigationFragment(this) .commit() } </think_never_used_51bce0c785ca2f68081bfa7d91973934>
相关产品推荐
相关产品推荐

