完整教程:Jetpack Navigation - Navigation 后台跳转观察记录(正常情况、回到桌面的情况、配合 currentDestination 观察)

准备阶段

1、Fragment Layout
  1. fragment_delay_jump_a.xml
<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 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"
    tools:context=".fragment.DelayJumpAFragment">
  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="DelayJumpAFragment" />
</FrameLayout>
  1. fragment_delay_jump_b.xml
<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 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"
    tools:context=".fragment.DelayJumpBFragment">
  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="DelayJumpBFragment" />
</FrameLayout>
2、Fragment Code
  1. DelayJumpAFragment.java
public class DelayJumpAFragment extends Fragment {
public static final String TAG = DelayJumpAFragment.class.getSimpleName();
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i(TAG, "onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment_delay_jump_a, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated");
...
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(TAG, "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.i(TAG, "onDetach");
}
}
  1. DelayJumpBFragment.java
public class DelayJumpBFragment extends Fragment {
public static final String TAG = DelayJumpBFragment.class.getSimpleName();
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i(TAG, "onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment_delay_jump_b, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(TAG, "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.i(TAG, "onDetach");
}
}
3、Navigation
  • delay_jump_graph.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/delay_jump_graph"
    app:startDestination="@id/delayJumpAFragment">
  <fragment
    android:id="@+id/delayJumpAFragment"
    android:name="com.my.navigation.fragment.DelayJumpAFragment"
    android:label="fragment_delay_jump_a"
    tools:layout="@layout/fragment_delay_jump_a" >
  <action
    android:id="@+id/action_delayJumpAFragment_to_delayJumpBFragment"
    app:destination="@id/delayJumpBFragment" />
</fragment>
<fragment
  android:id="@+id/delayJumpBFragment"
  android:name="com.my.navigation.fragment.DelayJumpBFragment"
  android:label="fragment_delay_jump_b"
  tools:layout="@layout/fragment_delay_jump_b" />
</navigation>
4、Activity Layout
  • activity_delay_jump.xml
<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DelayJumpActivity">
  <fragment
    android:id="@+id/nhf_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:navGraph="@navigation/delay_jump_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
5、Activity Code
  • DelayJumpActivity.java
public class DelayJumpActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_delay_jump);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}

一、Thread 后台跳转

1、具体实现
  • DelayJumpAFragment.java
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated");
Log.i(TAG, "10 秒后开始跳转");
new Thread(() -> {
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
getActivity().runOnUiThread(() -> {
Log.i(TAG, "开始跳转");
NavController navController = Navigation.findNavController(view);
navController.navigate(R.id.action_delayJumpAFragment_to_delayJumpBFragment);
});
}).start();
}
测试:正常情况
  1. 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
  1. 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
DelayJumpAFragment onPause
DelayJumpAFragment onStop
DelayJumpAFragment onDestroyView
  1. 跳转到 DelayJumpBFragment
# 输出结果
DelayJumpBFragment onAttach
DelayJumpBFragment onCreate
DelayJumpBFragment onCreateView
DelayJumpBFragment onViewCreated
DelayJumpBFragment onActivityCreated
DelayJumpBFragment onStart
DelayJumpBFragment onResume
测试:回到桌面的情况
  1. 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
  1. 回到桌面
# 输出结果
DelayJumpAFragment onPause
DelayJumpAFragment onStop
  1. 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
FragmentNavigator Ignoring navigate() call: FragmentManager has already saved its state
  1. 回到应用,并未跳转到 DelayJumpBFragment,仍然停留在 DelayJumpAFragment
# 输出结果
DelayJumpAFragment onStart
DelayJumpAFragment onResume

二、Handler 后台跳转

1、具体实现
  • DelayJumpAFragment.java
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated");
Log.i(TAG, "10 秒后开始跳转");
new Handler().postDelayed(() -> {
Log.i(TAG, "开始跳转");
NavController navController = Navigation.findNavController(view);
navController.navigate(R.id.action_delayJumpAFragment_to_delayJumpBFragment);
}, 10 * 1000);
}
测试:正常情况
  1. 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
  1. 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
DelayJumpAFragment onPause
DelayJumpAFragment onStop
DelayJumpAFragment onDestroyView
  1. 跳转到 DelayJumpBFragment
# 输出结果
DelayJumpBFragment onAttach
DelayJumpBFragment onCreate
DelayJumpBFragment onCreateView
DelayJumpBFragment onViewCreated
DelayJumpBFragment onActivityCreated
DelayJumpBFragment onStart
DelayJumpBFragment onResume
测试:回到桌面的情况
  1. 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
  1. 回到桌面
# 输出结果
DelayJumpAFragment onPause
DelayJumpAFragment onStop
  1. 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
FragmentNavigator Ignoring navigate() call: FragmentManager has already saved its state
  1. 回到应用,并未跳转到 DelayJumpBFragment,仍然停留在 DelayJumpAFragment
# 输出结果
DelayJumpAFragment onStart
DelayJumpAFragment onResume

配合 currentDestination 观察

1、具体实现
  • DelayJumpAFragment.java
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated");
navController = Navigation.findNavController(view);
Log.i(TAG, "10 秒后开始跳转");
new Thread(() -> {
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
getActivity().runOnUiThread(() -> {
Log.i(TAG, "开始跳转");
navController.navigate(R.id.action_delayJumpAFragment_to_delayJumpBFragment);
NavDestination currentDestination = navController.getCurrentDestination();
if (currentDestination == null) {
Log.i(TAG, "currentDestination 为 null");
return;
}
if (currentDestination.getId() == R.id.delayJumpAFragment) {
Log.i(TAG, "当前在 DelayJumpAFragment");
} else if (currentDestination.getId() == R.id.delayJumpBFragment) {
Log.i(TAG, "当前在 DelayJumpBFragment");
} else {
Log.i(TAG, "当前在其他 Fragment");
}
});
}).start();
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume");
NavDestination currentDestination = navController.getCurrentDestination();
if (currentDestination == null) {
Log.i(TAG, "currentDestination 为 null");
return;
}
if (currentDestination.getId() == R.id.delayJumpAFragment) {
Log.i(TAG, "当前在 DelayJumpAFragment");
} else if (currentDestination.getId() == R.id.delayJumpBFragment) {
Log.i(TAG, "当前在 DelayJumpBFragment");
} else {
Log.i(TAG, "当前在其他 Fragment");
}
}
测试:正常情况
  1. 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
DelayJumpAFragment 当前在 DelayJumpAFragment
  1. 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
DelayJumpAFragment 当前在 DelayJumpBFragment
DelayJumpAFragment onPause
DelayJumpAFragment onStop
DelayJumpAFragment onDestroyView
  1. 跳转到 DelayJumpBFragment
# 输出结果
DelayJumpBFragment onAttach
DelayJumpBFragment onCreate
DelayJumpBFragment onCreateView
DelayJumpBFragment onViewCreated
DelayJumpBFragment onActivityCreated
DelayJumpBFragment onStart
DelayJumpBFragment onResume
测试:回到桌面的情况
  1. 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
DelayJumpAFragment 当前在 DelayJumpAFragment
  1. 回到桌面
# 输出结果
DelayJumpAFragment onPause
DelayJumpAFragment onStop
  1. 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
FragmentNavigator Ignoring navigate() call: FragmentManager has already saved its state
DelayJumpAFragment 当前在 DelayJumpAFragment
  1. 回到应用,并未跳转到 DelayJumpBFragment,仍然停留在 DelayJumpAFragment
# 输出结果
DelayJumpAFragment onStart
DelayJumpAFragment onResume
DelayJumpAFragment 当前在 DelayJumpAFragment
posted @ 2026-01-29 13:21  clnchanpin  阅读(0)  评论(0)    收藏  举报