Android 如何在使用导航组件从应用程序注销时从登录退出应用程序?
下面是我的代码 navigation.xmlAndroid 如何在使用导航组件从应用程序注销时从登录退出应用程序?,android,kotlin,android-jetpack,android-jetpack-navigation,Android,Kotlin,Android Jetpack,Android Jetpack Navigation,下面是我的代码 navigation.xml <fragment android:id="@+id/HomeFragment" android:label="Home Screen"> <action android:id="@+id/action_activeRefillDeviceFragment_to_loginFragment" app:destination="@id/loginFragme
<fragment android:id="@+id/HomeFragment"
android:label="Home Screen">
<action
android:id="@+id/action_activeRefillDeviceFragment_to_loginFragment"
app:destination="@id/loginFragment"
app:popUpTo="@id/loginFragment"
app:popUpToInclusive="true"/>
</fragment>
<fragment android:id="@+id/loginFragment"
android:name="com.example.dprefilldevice.login.fragment.LoginFragment"
android:label="LoginFragment">
<action android:id="@+id/action_loginFragment_to_activeRefillDeviceFragment"
app:destination="@id/activeRefillDeviceFragment"
app:popUpTo="@id/loginFragment"
app:popUpToInclusive="true"/>
</fragment>
可能您应该尝试更改图形:将第一个操作更改为具有
app:poputo=“@id/HomeFragment”
。背后的想法是:从backstack中删除主片段,然后将登录片段放入backstack。我不建议强制完成您的活动。调用finish()
。
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.logout -> {
Utils.getPreferrence(context)?.edit()?.clear()?.apply()
findNavController().navigate(R.id.action_activeRefillDeviceFragment_to_loginFragment)
}
}
return super.onOptionsItemSelected(item)
}