问题描述
我使用的是 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" />
登录片段.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
}
所以,我在登录片段上打开应用程序,单击按钮,转到电子邮件登录片段,当我单击后退按钮时,我返回到设备的主屏幕。但问题是当我再次打开应用程序时,它以登录片段而不是电子邮件登录片段打开。
解决方法
听起来你对 popuptoIncusive 的定义有误。
popUpToInclusive="true" to indicate that the destination specified in app:popUpTo should also be removed from the back stack.
如果您在进入主屏幕时关闭应用程序,则导航组件的预期行为将是启动 Host 片段,在您的情况下是在片段中登录。
如果您在进入主屏幕时将应用程序发送到后台,并将应用程序带到前台,您仍将位于电子邮件片段中。