我之前发布了一个关于 NavHostFragment 无法在背压上工作的问题。任何人都没有回答这个问题,因此花了几天的时间,我终于设法解决了这个问题。(我只是在前面的问题中添加了新的代码并在此处更改了代码,以减少问题的长度)
导航图更改为以下内容:
<?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/launch_navigation_graph"
app:startDestination="@id/splashFragment">
<fragment android:id="@+id/splashFragment" android:name="com.myapp.android.SplashFragment"
android:label="fragment_splash" tools:layout="@layout/fragment_splash">
<action android:id="@+id/action_splashFragment_to_fragment1"
app:destination="@id/fragment1"
app:popUpTo="@+id/splashFragment"
app:popUpToInclusive="true"/>
</fragment>
<fragment android:id="@+id/fragment1"
android:name="com.myapp.android.Fragment1"
android:label="fragment1" tools:layout="@layout/fragment_register_msisdn">
<action android:id="@+id/action_fragment1_to_fragment2"
app:destination="@id/fragment2" app:popUpTo="@+id/fragment1"
app:popUpToInclusive="false"/>
</fragment>
<fragment android:id="@+id/fragment2"
android:name="com.myapp.android.Fragment2"
android:label="fragment_fragment2" tools:layout="@layout/fragment_fragment2"/>
</navigation>
上一个问题中缺少的一些信息。片段绑定之前是这样完成的(下面的代码是针对 Fragment1 的,在 Fragment2 中也是如此):
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: Fragment1Binding =
DataBindingUtil.setContentView(activity!!, R.layout.fragment1)
binding.lifecycleOwner = this
val fragment1ViewModel = ViewModelProviders.of(this).get(Fragment1ViewModel::class.java)
binding.viewModel = fragment1ViewModel
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment1, container, false)
}
上面的代码,带有导航图,现在不会崩溃,但后退按钮只是拒绝工作。它不会弹出片段,只需在第二次后按时退出。
现在我发现使用膨胀 UIDataBindingUtil.setContentView
是我的代码无法正常工作的原因。我通过逐步创建另一个示例项目来解决这个问题,直到我弄清楚它何时停止工作。我通过使用 DataBindingUtil 绑定到在 OnCreateView 中膨胀的视图,使示例项目使用数据绑定工作,如下所示。我删除了 oncreate 覆盖。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment1, container, false)
val binding = DataBindingUtil.bind<Fragment1Binding>(rootView)
binding?.lifecycleOwner = viewLifecycleOwner
val fragment1ViewModel = ViewModelProviders.of(this).get(Fragment1ViewModel::class.java)
binding.viewModel = fragment1ViewModel
return rootView
}
现在,此代码在示例项目中有效,但是当我将其放置在最终项目中时(在示例项目中,我只需放置按钮并单击以导航,就像在我的原始项目中一样,我有 web 服务调用并查看模型观察以进行导航),当从 Fragment2 向后按下时,上面的代码会崩溃并出现以下异常(如果我从 Fragment1 向后按下,Fragment1 也会发生类似的崩溃)。
2019-04-30 14:00:47.043 18087-18087/com.selfcare.safaricom E/InputEventSender: Exception dispatching finished signal.
2019-04-30 14:00:47.043 18087-18087/com.selfcare.safaricom E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-04-30 14:00:47.047 18087-18087/com.selfcare.safaricom E/MessageQueue-JNI: java.lang.IllegalArgumentException: navigation destination com.selfcare.safaricom:id/action_fragment1_to_fragment2 is unknown to this NavController
at androidx.navigation.NavController.navigate(NavController.java:803)
at androidx.navigation.NavController.navigate(NavController.java:744)
at androidx.navigation.NavController.navigate(NavController.java:730)
at androidx.navigation.NavController.navigate(NavController.java:718)
at com.myapp.android.Fragment2.handleLaunchStatus(Fragment2.kt:53)
at com.myapp.android.Fragment2.access$handleLaunchStatus(Fragment2.kt:18)
at com.myapp.android.Fragment2$attachLaunchObserver$1.onChanged(Fragment2.kt:46)
at com.myapp.android.Fragment2$attachLaunchObserver$1.onChanged(Fragment2.kt:18)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:424)
at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:376)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:188)
at androidx.lifecycle.LiveData.observe(LiveData.java:185)
at com.myapp.android.Fragment2.attachLaunchObserver(Fragment2.kt:45)
at com.myapp.android.Fragment2.onViewCreated(Fragment2.kt:37)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:895)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2092)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1822)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:298)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:241)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:288)
at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:241)
at androidx.fragment.app.FragmentActivity$1.handleOnBackPressed(FragmentActivity.java:144)
at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.java:136)
at androidx.activity.ComponentActivity.onBackPressed(ComponentActivity.java:283)
at android.app.Activity.onKeyUp(Activity.java:3083)
at android.view.KeyEvent.dispatch(KeyEvent.java:2716)
at android.app.Activity.dispatchKeyEvent(Activity.java:3366)
at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:80)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:98)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2736)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:342)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5037)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4905)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4585)
at android.view.ViewRootImpl$InputStage.apply(Vie
我用谷歌搜索了这个例外,但在这个特定的上下文中没有发现任何有用的东西可以解决我的问题。请帮忙。