Android Navigation xml属性研究
xml文件中控制navigation动作。
文件位置:res/navigation/filename.xml
语法:
<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"
app:startDestination="@[+]id/name">
<fragment
android:id="@+id/name"
android:name="[package:].name"
android:label="String"
tools:layout="@layout/layout_name" >
<action
android:id="@+id/name"
app:destination="@id/name"
app:launchSingleTop="[true|false]"
app:launchDocument="[true|false]"
app:clearTask="[true|false]"
app:popUpTo="@id/name"
app:popUpToInclusive="[true|false]"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"/>
</fragment>
<fragment
android:id="@+id/name"
android:name="[package:].name"
android:label="String"
tools:layout="@layout/layout_name" />
</navigation>
在使用过程中,使用最多的就是action标签,所以主要就分析它。
1、<navigation>
属性:
app:startDestination="@[+]id/name"
这就是代表一启动显示哪个元素,我这里使用的是fragment,所以后面的id填写的就是下面某个fragment的id,这样默认就会先显示这个fragment
2、<fragment>
属性:
android:id
id就是给这fragment取的唯一标识
android:name
这里填写的内容是你这个fragment的路径,这样系统才能通过这路径找到fragment类的实现(包名+具体fragment的类名)
android:label
给这fragment一标签,在有actionBar的情况下actionBar上的title会显示标签内容
3、<action>
属性:
android:id
给这action设置唯一标识
app:destination
这是这个action要跳转的目的地,需要填写上目的地的id。每一次跳转,会产生一个新的实例(Fragment1@01>Fragment1@02),这时Fragment1@01实例不会被销毁,只执行到onFragmentViewDestroyed
app:launchSingleTop
这是在跳转本身时保存单实例,就是说F1跳转F1,虽然会产生一个新的实例(Fragment1@01>Fragment1@02),但Fragment1@01实例会被销毁。Fragment1@01>Fragment2@02>Fragment1@03,这样的跳转不会起作用,Fragment1@01实例依旧存在。
app:launchDocument
具体效果不是很清楚,推荐过时
As per the {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} documentation, it is recommended to use {@link android.R.attr#documentLaunchMode} on an Activity you wish to launch as a new document.
这属性使用的官方翻译:
Launch a navigation target as a document if you want it to appear as its own entry in the system Overview screen. If the same document is launched multiple times it will not create a new task, it will bring the existing document task to the front.
<p>If the user presses the system Back key from a new document task they will land on their previous task. If the user reached the document task from the system Overview screen they will be taken to their home screen.</p>
如果您想让导航目标作为自己的条目出现在系统概述屏幕上,那么就将其作为文档启动。如果同一文档被多次启动,它将不会创建新的任务,它将把现有的文档任务放在前面。
<P>如果用户从一个新的文档任务中按下系统返回键,他们将在之前的任务中着陆。如果用户从系统概述屏幕到达文档任务,则会将其带到主屏幕</p>
app:clearTask
为true会清空栈中的元素。Fragment1@01>Fragment2@02>Fragment3@03,
这时Fragment3启动Fragment4的action设置这标记为true(Fragment3@03>Fragment4@04),此时Fragment1、Fragment2、Fragment3实例统统销毁,栈中只存Fragment4@04。官方不推荐,推荐使用app:popUpTo="@id/name" app:popUpToInclusive="[true|false]"
这两属性组合。
app:popUpTo
这是出栈直到某个元素。Fragment1@01>Fragment2@02>Fragment3@03,在Fragment3启动Fragment4时设置出栈到Fragment1,那栈中的Fragment2,Fragment3会出栈销毁,只存Fragment1和Fragment4。
app:popUpToInclusive
这属性配合app:popUpTo
使用,用来判断到达指定元素时是否把指定元素也出栈。同样上面的例子,true的话Fragment1也会出栈销毁,栈中只存留Fragment4。
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
前两个是移动到目的地的动画,后两个是离开目的地的动画。
举个例子:Fragment1跳转Fragment2
app:enterAnim
是这个action目的地进入的动画,是Fragment2入场的动画。
app:exitAnim
是这个action所在元素离开的动画,是Fargment1离场的动画。
app:popEnterAnim
是这个按下返回动作,目的地出栈时,action所在元素入场的动画,是Fargment1入场的动画。
app:popExitAnim
是这个按下返回动作,目的地出栈时离开的动画,是Fragment2离场的动画。