我正在接收来自Firebase的通知,如果在前台或后台收到通知,它可以正确导航。但是,如果我从阻止屏幕上点击通知,则它不起作用。我认为问题出在找不到navController。但是不知道该怎么办
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.MainTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
mainviewModel.navigateToMainFragmentEvent.observe(this, EventObserver {
Log.d("A","notification")`
Navigation.findNavController(this, R.id.nav_host_fragment)
.navigate(R.id.action_global_mainFragment)
})
}
}
SplashFragment是homeFragment
class SplashFragment : Fragment(R.layout.activity_splash) {
val splashViewModel: SplashViewModel by sharedViewModel()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getStringExtra(NOTIFICATION_TYPE)?.let {
splashViewModel.notificationRecived(it)
}
}
}
当我调试此代码时:
Navigation.findNavController(this, R.id.nav_host_fragment)
IllegalStateException:ID未引用此活动内的视图