Android Jetpack-Navigation(一)
1 Introduction
Navigation is a library and a plug-in simplifies navigation of Android.
Navigation Fragment is used to manage the handover, and can be visualized by the way, we see the interaction process App.
2. advantage
- The process switching Fragment
- By default, the correct handling of Fragment forward and back
- Provides standardized transition and animation resources
- Implementation and handling deep connection
- You can bind Toolbar, BottomNavigationView and ActionBar
- ViewModel support
3. Prepare
AndroidStudio 3.2 or higher
4. Navigation three elements
noun | Explanation |
---|---|
Nvigtiodargrf | This is a new resource file, the user can see the Destination it can reach (the user can reach the screen interface), as well as the relationship between processes visual interface |
NavHostFragment | The current fragment of container |
NavController | Navigation controller |
5. Use
1. Add dependence
implementation group: 'androidx.navigation', name: 'navigation-fragment', version: '2.2.0'
implementation group: 'androidx.navigation', name: 'navigation-ui', version: '2.2.0'
2. Create NavigationGraph
Create a new file in the directory res folder navigation, right-click on the Create New Navigation resource file.
Destinations:
HOST: i.e. on the table NavHostFragment, i.e. containers fragment
GRAPH: f is the current vessel fragment
app:startDestination
The default starting position
android:name
: Fragment of the full package name
tools:layout
: Fragment layout file
action
android:id
: References this id in the jump fragment of
android:destination
: Destination, on behalf of this action is to jump to the destination from this fragment represented by fragment
android:popUpTo
: This represents, the stack until specified fragment popUpTo
3. Establish NavHostFragment
That is the second step of HOST, which is the current fragment of container
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_activity"
app:navGraph = "@navigation/welcome_navigation"
app:defaultNavHost = "true"
android:name="androidx.navigation.fragment.NavHostFragment"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Attributes | Explanation |
---|---|
android:name | The value must be androidx.navigation.fragment.NavHostFragment |
android:navGraph | 存放的是第二步建好的导航资源文件,也就是确定了NavigationGraph |
app:defaultNavHost=“true” | 与系统的返回按钮相关联 |
6. 处理跳转
跳转通过NavController对象,它有三种获取方法:
NavHostFragment.findNavController(Fragment);
Navigation.findController(Activity,@IdRes int viewId);
Navigation.findController(View);
调用NavController的navigate方法执行跳转,navigate的参数可以是一个destination(这里就是fragment在导航图nav_graph中的id),也可以是action的id
7. 演示
welcome_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/welcome_navigation"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.example.navigationproject.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first"
>
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:enterAnim="@anim/fragment_close_enter"
app:exitAnim="@anim/fragment_close_exit"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
<action
android:id="@+id/action_firstFragment_to_thirdFragment"
app:destination="@id/thirdFragment"
/>
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.navigationproject.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_secondFragment_to_blankFragment2"
app:destination="@id/blankFragment2"
app:enterAnim="@anim/fragment_close_enter"
app:exitAnim="@anim/fragment_close_exit"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/thirdFragment"
android:name="com.example.navigationproject.ThirdFragment"
android:label="fragment_third"
tools:layout="@layout/fragment_third" />
<fragment
android:id="@+id/blankFragment2"
android:name="com.example.navigationproject.BlankFragment"
android:label="fragment_blank"
tools:layout="@layout/fragment_blank" >
<action
android:id="@+id/backToFirst"
app:popUpTo="@id/firstFragment"/>
<action
android:id="@+id/aBackToFirst"
app:destination="@id/firstFragment"
app:enterAnim="@anim/fragment_close_enter"
app:exitAnim="@anim/fragment_close_exit"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
</navigation>
主要通过上面的一条线来演示fragment的切换
firstfragment:点击第一个button进入到secondfragment
secondfragment:点击textview进入到blankfragment2
blankfragment2:点击第一个textview通过android:popupto的方式跳转到firstfragment
点击第二个text view通过android:destination的方式跳转到firstfragment
在每个fragment中我们都重写了onDestroy方法,并在控制台打印log
当我们点击了blankfragment2的第一个textview的时候,可以看到blankfragment2和secondfragment都执行了onDestroy方法并且看界面回到了firstfragment
当我们点击blankfragment2的第二个textview的时候,可以看到控制台并没有打印什么,也就是说此时fragment容器中共有四个fragment:firstfragment-secondfragment-blankfragment2-firstfragment
app:defaultNavHost="true"的作用
表示 NavHostFragment 可以拦截处理返回键。
If set to false, then, when you click the back key, the effect is to switch activity rather than fragment