I had posted a question之前是关于NavHostFragment无法在反压模式下工作的。这个问题没有被任何人回答,因此花了几天的时间,我终于设法弄清楚了这个问题。(我只是添加了先前问题中缺少的新代码,并在此处更改了代码,以减少问题的长度)< / p>
导航图更改为以下内容:
<?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)
}
上面的代码以及导航图现在不会崩溃,但是返回按钮只是拒绝工作。它不会弹出片段,只需在第二次按下时退出即可。
现在,我发现使用DataBindingUtil.setContentView
使UI膨胀是我的代码无法正常工作的原因。我通过逐步创建另一个示例项目来弄清楚,直到弄清楚它何时停止工作。我通过使用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
我用谷歌搜索了一个例外,但是发现在这种可以解决我的问题的特殊情况下没有任何用处。请帮忙。