假定场景是:
程序启动 -- Activity_A
Activity_A中有两个按钮,btn_A和btn_B;
btn_A点击跳转到Fragment_B, 点击Fragment_B中的按钮btn_A跳转到ActivityC中
btn_B点击跳转到Activity_B中;
1. 先集成依赖:
主工程的build.gradle中的buildScript中的 dependencies中加入: classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0"
在当前module中的build.gradle中加入:
//以下两个是写java用的依赖
implementation "androidx.navigation:navigation-fragment:2.3.0"
implementation "androidx.navigation:navigation-ui:2.3.0"
//以下两个是写kotlin用的依赖;
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
(好像选哪两个都无所谓,都一样?)
2. 分别把 Fragment_A、Fragment_B、Activity_A、Activity_B、Activity_C的代码逻辑 和 布局都写好;
其中,让Activity_A中的布局包含Fragment_A,并如下所示:
<!-- 这是Activity_A 对应的layout文件 -->
<?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"
tools:context=".Activity_A">
<fragment
android:id="@+id/fragment_a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_action" /> <!-- 最后这三行很关键 -->
</androidx.constraintlayout.widget.ConstraintLayout>
对应的Activity_A代码如下:
class Activity_A : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_a)
}
}
Activity_B、Activity_C没什么代码,跟上面的Activity_A代码基本一样,布局随便写,因为演示的效果中没什么逻辑;
3. 在module中的res下面创建一个navigation文件夹,和对应的xml,用于写跳转导向。
可以这么创建:
对 res 右键 --->
New --->
Android Resource File --->
在Resource type这一栏中选择Navigation(这样能主动创建navigation),然后在File Name中写上步骤2中声明的 nav_action即可。
其中nav_action.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/nav_grash"
app:startDestination="@id/fragment_a">
<fragment
android:id="@+id/fragment_a"
android:name="com.xdja.usejetpack.Fragment_A"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/action_fragment_a_to_fragment_b"
app:destination="@id/fragment_b" />
<action
android:id="@+id/action_fragment_a_to_activity_b"
app:destination="@id/activity_b" />
</fragment>
<fragment
android:id="@+id/fragment_b"
android:name="com.xdja.usejetpack.Fragment_B"
tools:layout="@layout/fragment_b">
<action
android:id="@+id/action_fragment_b_to_activity_c"
app:destination="@id/activity_c" />
</fragment>
<activity
android:id="@+id/activity_b"
android:name="com.xdja.usejetpack.Activity_B"></activity>
<activity
android:id="@+id/activity_c"
android:name="com.xdja.usejetpack.Activity_C"></activity>
</navigation>
写完之后,在android studio点击 右上角的 Split 或者Design 能看到:
做过ios开发的,感觉是不是有点眼熟?不过这个没有ios拖拽那么方便。
4. Fragment_A的代码如下所示:
public class Fragment_A extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
final NavController navController = NavHostFragment.findNavController(this);
View view = getView().findViewById(R.id.btn_first);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navController.navigate(R.id.action_fragment_a_to_activity_b);
}
});
View button = getView().findViewById(R.id.btn_second);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navController.navigate(R.id.action_fragment_a_to_fragment_b);
}
});
}
}
其中fragmen_a.xml的代码如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/fragment_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragment_A">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome! 当前是 Fragment A" />
<Button
android:id="@+id/btn_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到Activity_B"
android:textAllCaps="false" />
<Button
android:id="@+id/btn_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到Fragment_B"
android:textAllCaps="false" />
</LinearLayout>
Fragment_B的代码:
public class Fragment_B extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_b, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
final NavController navController = NavHostFragment.findNavController(this);
getView().findViewById(R.id.btn_second)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navController.navigate(R.id.action_fragment_b_to_activity_c);
getActivity().finish();
}
});
}
}
fragment_b.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前是 Fragment_B" />
<Button
android:id="@+id/btn_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到Activity_C"
android:textAllCaps="false" />
</LinearLayout>