为什么在android应用程序中使用导航组件两次调用onViewCreated
我有一个简单的应用程序,有一个活动和大约4个片段。我正在使用android导航组件。每次更改片段时,从我的初始片段开始到下一个片段,OnCreateview和onViewCreated会被调用两次,一次是在最初创建片段时,第二次是在我使用为什么在android应用程序中使用导航组件两次调用onViewCreated,android,android-architecture-navigation,android-navigation-graph,Android,Android Architecture Navigation,Android Navigation Graph,我有一个简单的应用程序,有一个活动和大约4个片段。我正在使用android导航组件。每次更改片段时,从我的初始片段开始到下一个片段,OnCreateview和onViewCreated会被调用两次,一次是在最初创建片段时,第二次是在我使用findnavController.navigate()时。我不知道为什么叫它两次,有人能说明原因吗。 这是我的飞溅课 class SplashFragment : Fragment(), SocketConnection.SocketCallbacks {
findnavController.navigate()时。我不知道为什么叫它两次,有人能说明原因吗。
这是我的飞溅课
class SplashFragment : Fragment(), SocketConnection.SocketCallbacks {
lateinit var binding: SplashFragmentBinding
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Timber.d("on View created")
SocketConnection.getInstance.socketConnectionListener(this)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Timber.d("on create view")
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_splash, container, false)
return binding.root
}
override fun onResume() {
super.onResume()
Timber.d("On resume")
activity?.setupBackButton(false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.d("On create")
setHasOptionsMenu(false)
}
override fun onConnection(param: String) {
Timber.d("Items received")
findNavController().navigate(R.id.action_splashFragment_to_privacyFragment, bundle)
}
这是MainActivity的xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的导航图
<?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/nav_graph"
app:startDestination="@id/splashFragment">
<fragment
android:id="@+id/splashFragment"
android:name="com.appname.ui.SplashFragment"
android:label="fragment_splash"
tools:layout="@layout/fragment_splash">
<action
android:id="@+id/action_splashFragment_to_voiceChatFragment"
app:destination="@id/voiceChatFragment"
app:launchSingleTop="true"
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_splashFragment_to_registerFragment2"
app:destination="@id/registerFragment2"
app:launchSingleTop="true"
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_splashFragment_to_privacyFragment"
app:destination="@id/privacyFragment"
app:launchSingleTop="true"
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/registerFragment2"
android:name="com.appname.ui.RegisterFragment"
android:label="fragment_register"
tools:layout="@layout/fragment_register" >
<action
android:id="@+id/action_registerFragment2_to_voiceChatFragment"
app:destination="@id/voiceChatFragment"
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true"
app:launchSingleTop="true" />
</fragment>
<fragment
android:id="@+id/privacyFragment"
android:name="com.appname.ui.PrivacyFragment"
android:label="PrivacyFragment" >
<action
android:id="@+id/action_privacyFragment_to_registerFragment2"
app:destination="@id/registerFragment2"
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true"
app:launchSingleTop="true" />
<action
android:id="@+id/action_privacyFragment_to_voiceChatFragment"
app:destination="@id/voiceChatFragment"
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true"
app:launchSingleTop="true" />
</fragment>
<fragment
android:id="@+id/voiceChatFragment"
android:name="com.appname.ui.VoiceChatFragment"
android:label="VoiceChatFragment" />
<fragment
android:id="@+id/profileFragment"
android:name="com.appname.ui.ProfileFragment"
android:label="ProfileFragment" />
</navigation>
我见过很多这样的问题,但它们都与片段管理器有关。但我使用的是android导航组件。我无法确定为什么要调用两次。谁能给我指出正确的方向,或者指出问题是由代码提供的。不确定它是否解决了问题,但我正在努力解决类似的问题。在我的方法中,我尝试使用NavigationAdvancedSample实现导航,以保存由底部导航()管理的堆栈。如果您使用类似的方法,您可以检查设置app:navGraph=“FragmentContainerView
中的@navigation/nav_graph
是必需的。在我的例子中,删除它解决了问题,因为我使用BottomNavigationView
中的setupWithNavController
方法在主活动中设置导航图。您应该在布局或活动中设置一次导航图。如果错误地设置了两个位置,它将创建startDestination片段两次
//In the host fragment
app:navGraph="@navigation/<your navigation graph>"
//在主机片段中
app:navGraph=“@navigation/”
或
//在活动中
findNavController()。图形=
当您按下后退按钮进入上一个片段时,是否会发生这种情况?请注意,当您回来时,片段将被重新创建,因此它将调用onCreateView、onViewCreated方法。我认为您没有正确理解这个问题。调用OnCreateView两次,而不按back按钮或任何其他事件。它被调用了两次,一次是在片段最初创建时,另一次是在findNavController.navigate事件之后。您找到这个问题的答案了吗?-我看到了一个类似的问题
//In the activity
findNavController().graph = <your navigation graph>