Android Jetpack架构组件(六)Navigation简单使用

参考:
https://developer.android.google.cn/guide/navigation/
https://developer.android.google.cn/guide/navigation/navigation-getting-started
https://blog.csdn.net/hongxue8888/article/details/103672351
https://www.jianshu.com/p/0bb65607bdf8

阐述Navigation如何管理MainActivity中的NavigationPage1FragmentNavigationPage2FragmentNavigationPage3Fragment三个fragment。

Gradle依赖

1
2
3
4
def nav_version = "2.3.0-alpha06"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"

在app的res目录下,创建一个navigation文件夹,在此文件夹下新增一个navigation的xml文件,此处新增一个nav_graph_main.xml,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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/nav_graph_main.xml"
app:startDestination="@id/page1Fragment">

<fragment
android:id="@+id/page1Fragment"
android:name="cn.appblog.jetpack.navigation.fragment.NavigationPage1Fragment"
android:label="fragment_page1"
tools:layout="@layout/fragment_navigation_page1">
<!--
action:程序中使用id跳到destination对应的类
-->
<action
android:id="@+id/action_page2"
app:destination="@id/page2Fragment" />
</fragment>

<fragment
android:id="@+id/page2Fragment"
android:name="cn.appblog.jetpack.navigation.fragment.NavigationPage2Fragment"
android:label="fragment_page2"
tools:layout="@layout/fragment_navigation_page2">
<action
android:id="@+id/action_page1"
app:destination="@id/page1Fragment" />
<action
android:id="@+id/action_page3"
app:destination="@id/page3Fragment" />
</fragment>

<!--<navigation-->
<!-- android:id="@+id/nav_graph_page3"-->
<!-- app:startDestination="@id/page3Fragment">-->
<fragment
android:id="@+id/page3Fragment"
android:name="cn.appblog.jetpack.navigation.fragment.NavigationPage3Fragment"
android:label="fragment_page3"
tools:layout="@layout/fragment_navigation_page3">
<action
android:id="@+id/action_page2"
app:destination="@id/page2Fragment" />
</fragment>

<!--</navigation>-->

</navigation>

在Design中可以看到非常直观的fragment切换流程,如下:

nav_graph_main

主页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<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"
>

<!--
app:defaultNavHost="true"
拦截系统back键,在fragment中就可以用back回退上一个fragment
app:navGraph="@navigation/nav_graph_main"
连接使用nav_graph_main.xml,获取fragment之间的导航结构
-->
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph_main" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • app:defaultNavHost="true"

拦截系统back键,在fragment中就可以用back回退上一个fragment

  • app:navGraph="@navigation/nav_graph_main"

连接使用nav_graph_main.xml,获取fragment之间的导航结构

  • android:name="androidx.navigation.fragment.NavHostFragment"

name 必须指定,表示这是一个可以切换fragment的控件

三个Fragment

Navigation.findNavController(view).navigate(R.id.action_page2);

实现点击按钮后Fragment页面的跳转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class NavigationPage1Fragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_navigation_page1, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btn = view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_page2);
}
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class NavigationPage2Fragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_navigation_page2, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btn = view.findViewById(R.id.btn);
//返回MainPage1Fragment
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_page1);
}
});

Button btn2 = view.findViewById(R.id.btn2);
//前往MainPage3Fragment
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_page3);
}
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class NavigationPage3Fragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_navigation_page3, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btn = view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_page2);
//回退上一步
// Navigation.findNavController(view).navigateUp();
}
});
}
}

遗留问题

尽管通过Navigation方式管理Fragment的代码简洁而且直观,但是有一个非常致命的问题,就是同一个Fragment会被重复创建,这样会导致每次切换Fragment的时候都会重新去请求数据,非常浪费系统资源,而且降低了APP的用户体验。

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :