导航 PopUpToInclusive 是否有效?重新打开应用程序时,它会打开登录片段而不是电子邮件登录片段
Navigation PopUpToInclusive doenst work? When the app is reopened, it opens the Login Fragment instead of Email Login Fragment
我正在使用 android Jetpack 导航,应用程序的流程是:
登录屏幕 → 电子邮件登录屏幕
nav_main.xml
<fragment android:id="@+id/loginFragment"
android:name="com.example.myapp.ui.main.LoginFragment"
android:label="@string/login"
tools:layout="@layout/fragment_login" >
<action
android:id="@+id/action_login_to_emailLoginFragment"
app:destination="@id/emailLoginFragment"
app:popEnterAnim="@anim/slide_in_right"
app:popExitAnim="@anim/slide_out_right"
app:popUpTo="@+id/loginFragment"
app:popUpToInclusive="true"/>
</fragment>
<fragment android:id="@+id/emailLoginFragment"
android:name="com.example.myapp.ui.main.EmailLoginFragment"
android:label="EmailLoginFragment"
tools:layout="@layout/fragment_login_email" />
LoginFragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding.emailLoginButton.setOnClickListener {
findNavController().navigate(R.id.action_login_to_emailLoginFragment)
}
return binding.root
}
所以,我在登录片段上打开应用程序,单击按钮,转到电子邮件登录片段,当我单击后退按钮时,我 return 回到设备的主屏幕。但问题是当我再次打开应用程序时,它会在登录片段而不是电子邮件登录片段中打开。
听起来您对 popuptoIncusive 的功能定义有误。
popUpToInclusive="true" to indicate that the destination specified in app:popUpTo should also be removed from the back stack.
如果您在转到主屏幕时关闭应用程序,则导航组件的预期行为是启动主机片段,在您的情况下是登录片段。
如果您在转到主屏幕时将应用程序发送到后台,然后将应用程序带到前台,您仍然会在电子邮件片段中。
我正在使用 android Jetpack 导航,应用程序的流程是: 登录屏幕 → 电子邮件登录屏幕
nav_main.xml
<fragment android:id="@+id/loginFragment"
android:name="com.example.myapp.ui.main.LoginFragment"
android:label="@string/login"
tools:layout="@layout/fragment_login" >
<action
android:id="@+id/action_login_to_emailLoginFragment"
app:destination="@id/emailLoginFragment"
app:popEnterAnim="@anim/slide_in_right"
app:popExitAnim="@anim/slide_out_right"
app:popUpTo="@+id/loginFragment"
app:popUpToInclusive="true"/>
</fragment>
<fragment android:id="@+id/emailLoginFragment"
android:name="com.example.myapp.ui.main.EmailLoginFragment"
android:label="EmailLoginFragment"
tools:layout="@layout/fragment_login_email" />
LoginFragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding.emailLoginButton.setOnClickListener {
findNavController().navigate(R.id.action_login_to_emailLoginFragment)
}
return binding.root
}
所以,我在登录片段上打开应用程序,单击按钮,转到电子邮件登录片段,当我单击后退按钮时,我 return 回到设备的主屏幕。但问题是当我再次打开应用程序时,它会在登录片段而不是电子邮件登录片段中打开。
听起来您对 popuptoIncusive 的功能定义有误。
popUpToInclusive="true" to indicate that the destination specified in app:popUpTo should also be removed from the back stack.
如果您在转到主屏幕时关闭应用程序,则导航组件的预期行为是启动主机片段,在您的情况下是登录片段。
如果您在转到主屏幕时将应用程序发送到后台,然后将应用程序带到前台,您仍然会在电子邮件片段中。