准备阶段
1、Fragment Layout
- 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>
- 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
- 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");
}
}
- 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
<?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
<?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
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、具体实现
@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();
}
测试:正常情况
- 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
- 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
DelayJumpAFragment onPause
DelayJumpAFragment onStop
DelayJumpAFragment onDestroyView
- 跳转到 DelayJumpBFragment
# 输出结果
DelayJumpBFragment onAttach
DelayJumpBFragment onCreate
DelayJumpBFragment onCreateView
DelayJumpBFragment onViewCreated
DelayJumpBFragment onActivityCreated
DelayJumpBFragment onStart
DelayJumpBFragment onResume
测试:回到桌面的情况
- 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
- 回到桌面
# 输出结果
DelayJumpAFragment onPause
DelayJumpAFragment onStop
- 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
FragmentNavigator Ignoring navigate() call: FragmentManager has already saved its state
- 回到应用,并未跳转到 DelayJumpBFragment,仍然停留在 DelayJumpAFragment
# 输出结果
DelayJumpAFragment onStart
DelayJumpAFragment onResume
二、Handler 后台跳转
1、具体实现
@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);
}
测试:正常情况
- 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
- 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
DelayJumpAFragment onPause
DelayJumpAFragment onStop
DelayJumpAFragment onDestroyView
- 跳转到 DelayJumpBFragment
# 输出结果
DelayJumpBFragment onAttach
DelayJumpBFragment onCreate
DelayJumpBFragment onCreateView
DelayJumpBFragment onViewCreated
DelayJumpBFragment onActivityCreated
DelayJumpBFragment onStart
DelayJumpBFragment onResume
测试:回到桌面的情况
- 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
- 回到桌面
# 输出结果
DelayJumpAFragment onPause
DelayJumpAFragment onStop
- 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
FragmentNavigator Ignoring navigate() call: FragmentManager has already saved its state
- 回到应用,并未跳转到 DelayJumpBFragment,仍然停留在 DelayJumpAFragment
# 输出结果
DelayJumpAFragment onStart
DelayJumpAFragment onResume
配合 currentDestination 观察
1、具体实现
@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");
}
}
测试:正常情况
- 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
DelayJumpAFragment 当前在 DelayJumpAFragment
- 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
DelayJumpAFragment 当前在 DelayJumpBFragment
DelayJumpAFragment onPause
DelayJumpAFragment onStop
DelayJumpAFragment onDestroyView
- 跳转到 DelayJumpBFragment
# 输出结果
DelayJumpBFragment onAttach
DelayJumpBFragment onCreate
DelayJumpBFragment onCreateView
DelayJumpBFragment onViewCreated
DelayJumpBFragment onActivityCreated
DelayJumpBFragment onStart
DelayJumpBFragment onResume
测试:回到桌面的情况
- 应用启动,DelayJumpAFragment 启动
# 输出结果
DelayJumpAFragment onAttach
DelayJumpAFragment onCreate
DelayJumpAFragment onCreateView
DelayJumpAFragment onViewCreated
DelayJumpAFragment 10 秒后开始跳转
DelayJumpAFragment onActivityCreated
DelayJumpAFragment onStart
DelayJumpAFragment onResume
DelayJumpAFragment 当前在 DelayJumpAFragment
- 回到桌面
# 输出结果
DelayJumpAFragment onPause
DelayJumpAFragment onStop
- 开始跳转
# 输出结果
DelayJumpAFragment 开始跳转
FragmentNavigator Ignoring navigate() call: FragmentManager has already saved its state
DelayJumpAFragment 当前在 DelayJumpAFragment
- 回到应用,并未跳转到 DelayJumpBFragment,仍然停留在 DelayJumpAFragment
# 输出结果
DelayJumpAFragment onStart
DelayJumpAFragment onResume
DelayJumpAFragment 当前在 DelayJumpAFragment