调用顺序:
HomeFragment-CategoryFragment-TagsFragment(循环调用)
调用代码
Home:
findNavController.navigate(R.id.category_fragment)
Category:
navController.navigate(
R.id.tags_fragment,
null,
NavOptions.Builder()
.setPopUpTo(
destinationId = R.id.home_fragment,
inclusive = true,
saveState = true
).build()
)
Tags:
navController.navigate(R.id.home_fragment,null,NavOptions.Builder().setRestoreState(true).build())
日志:
启动:
HomeFragment-108038523-onCreate: null
HomeFragment-108038523-onResume:
第一次点击:
HomeFragment-108038523-onPause:
CategoryFragment-110566080-onCreate: null
HomeFragment-108038523-onDestroyView:
CategoryFragment-110566080-onResume:
第二次点击:
HomeFragment-108038523-onDestroy:
CategoryFragment-110566080-onPause:
CategoryFragment-110566080-onSaveInstanceState: 开始保存
TagsFragment-89193174-onCreate: null
CategoryFragment-110566080-onDestroyView:
CategoryFragment-110566080-onDestroy:
TagsFragment-89193174-onResume:
以上一切正常
第三次点击:
TagsFragment-89193174-onPause:
HomeFragment-9554669-onCreate: null
CategoryFragment-159926621-onCreate:xxxx(有值)
TagsFragment-89193174-onDestroyView:
CategoryFragment-159926621-onResume:
第四次点击:
HomeFragment-9554669-onSaveInstanceState: 开始保存
HomeFragment-9554669-onDestroy:
CategoryFragment-159926621-onPause:
CategoryFragment-159926621-onSaveInstanceState: 开始保存
TagsFragment-195906585-onCreate: null
CategoryFragment-159926621-onDestroyView:
CategoryFragment-159926621-onDestroy:
TagsFragment-195906585-onResume:
疑问:
1.在Category当中调用inclusive包含目标出栈,saveState保存状态,
HomeFragment和CategoryFragment正常退出,
但是我的保存状态saveState是设置为true,
日志中可以看见只有CategoryFragment调用了onSaveInstanceState,为什么Home没有调onSaveInstanceState?saveState已经设置为true了,这样做不会保存目标栈的状态吗?
2.在第三次点击点击当中,在代码中可以看到我目标页面是HomeFragment,
可是为什么HomeFragment只做了创建并没有显示,而且还把HomeFragment下一级的CategoryFragment出来了,再到第四次点击的时候已经到了CategoryFragment代码当中导航导TagsFragment,HomeFragment和CategoryFragment出栈了这没有问题,可是这个时候HomeFragment又开始保存状态了呢?