build.gradle
中引入相关依赖implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
在 NormalOneFragment 中,可以前进到 NormalTwoFragment
在 NormalTwoFragment 中,可以后退到 NormalOneFragment
<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=".fragment.NormalOneFragment"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NormalOneFragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="前进"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragment.NormalTwoFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NormalTwoFragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="后退"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
androidx.constraintlayout.widget.ConstraintLayout>
package com.my.navigation.fragment;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.my.navigation.R;
public class NormalOneFragment extends Fragment {
private NavController navController;
private Button btnGo;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_normal_one, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
navController = Navigation.findNavController(view);
btnGo = view.findViewById(R.id.btn_go);
btnGo.setOnClickListener(v -> navController.navigate(R.id.action_normalOneFragment_to_normalTwoFragment));
}
}
package com.my.navigation.fragment;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.my.navigation.R;
public class NormalTwoFragment extends Fragment {
private NavController navController;
private Button btnBack;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_normal_two, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
navController = Navigation.findNavController(view);
btnBack = view.findViewById(R.id.btn_back);
btnBack.setOnClickListener(v -> navController.navigateUp());
}
}
创建 Navigation Graph 文件:右击 res 目录 -> 【New】 -> 【Android Recourse File】
完成操作后,Android Studio 会自动在 res 目录下创建 navigation 目录,并该目录下创建 Navigation Graph 文件
<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/my_graph_normal"
app:startDestination="@id/normalOneFragment">
<fragment
android:id="@+id/normalOneFragment"
android:name="com.my.navigation.fragment.NormalOneFragment"
android:label="fragment_normal_one"
tools:layout="@layout/fragment_normal_one" >
<action
android:id="@+id/action_normalOneFragment_to_normalTwoFragment"
app:destination="@id/normalTwoFragment" />
fragment>
<fragment
android:id="@+id/normalTwoFragment"
android:name="com.my.navigation.fragment.NormalTwoFragment"
android:label="fragment_normal_two"
tools:layout="@layout/fragment_normal_two" />
navigation>
<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=".NormalActivity">
<fragment
android:id="@+id/nhf"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/my_graph_normal" />
androidx.constraintlayout.widget.ConstraintLayout>
package com.my.navigation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class NormalActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
}
}
android:id="@+id/my_graph_normal"
:导航图的唯一标识符
app:startDestination="@id/normalOneFragment"
:当导航图被加载时应该首先显示的 Fragment
<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/my_graph_normal"
app:startDestination="@id/normalOneFragment">
navigation>
android:id="@+id/normalOneFragment"
:Fragment 的唯一标识符
android:name="com.my.navigation.fragment.NormalOneFragment"
:指定 Fragment 的类,应用会创建它
android:label="fragment_normal_one"
:Fragment 的标签,通常用于调试或 UI 显示
tools:layout="@layout/fragment_normal_one"
:指定 Fragment 的布局文件,仅用于 Android Studio 的预览功能
<fragment
android:id="@+id/normalOneFragment"
android:name="com.my.navigation.fragment.NormalOneFragment"
android:label="fragment_normal_one"
tools:layout="@layout/fragment_normal_one" >
<action
android:id="@+id/action_normalOneFragment_to_normalTwoFragment"
app:destination="@id/normalTwoFragment" />
fragment>
<fragment
android:id="@+id/normalTwoFragment"
android:name="com.my.navigation.fragment.NormalTwoFragment"
android:label="fragment_normal_two"
tools:layout="@layout/fragment_normal_two" />
android:id="@+id/action_normalOneFragment_to_normalTwoFragment"
:导航动作的唯一标识符
app:destination="@id/normalTwoFragment"
:指定导航的目的地 Fragment
<action
android:id="@+id/action_normalOneFragment_to_normalTwoFragment"
app:destination="@id/normalTwoFragment" />
android:id="@+id/nhf"
:NavHostFragment 的唯一标识符
android:name="androidx.navigation.fragment.NavHostFragment"
:指定 NavHostFragment 的类,应用会创建它
app:navGraph="@navigation/my_graph_normal"
:指定与 NavHostFragment 关联的导航图的资源 ID
<fragment
android:id="@+id/nhf"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/my_graph_normal" />
// 在 Fragment 中,通常在其 onViewCreated 方法或获取
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NavController navController = Navigation.findNavController(view);
}
// 在 Activity 中,通常在其 onCreate 方法或获取
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
NavController navController = Navigation.findNavController(this, R.id.nhf);
}
R.id.action_moreJumpOneFragment_to_moreJumpTwoFragment
是导航动作的唯一标识符navController.navigate(R.id.action_moreJumpOneFragment_to_moreJumpTwoFragment)
navController.navigateUp()