Mặc dù đây là một câu hỏi cũ đã được trả lời, nhưng có lẽ những ví dụ tiếp theo có thể bổ sung cho câu trả lời được chấp nhận và chúng có thể hữu ích cho một số lập trình viên mới trong Android như tôi.
Tùy chọn 1 - "addToBackStack ()" không bao giờ được sử dụng
Trường hợp 1A - thêm, xóa và nhấp vào nút Quay lại
Activity : onCreate() - onStart() - onResume() Activity is visible
add Fragment A : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment A is visible
add Fragment B : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment B is visible
add Fragment C : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
remove Fragment C : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Fragment B is visible
(Back button clicked)
Activity : onPause() - onStop() - onDestroy()
Fragment A : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
Fragment B : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() App is closed, nothing is visible
Trường hợp 1B - thêm, thay thế và nhấp vào nút Quay lại
Activity : onCreate() - onStart() - onResume() Activity is visible
add Fragment A : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment A is visible
add Fragment B : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment B is visible
(replace Fragment C)
Fragment B : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
Fragment A : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
Fragment C : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
(Back button clicked)
Activity : onPause() - onStop() - onDestroy()
Fragment C : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() App is closed, nothing is visible
Tùy chọn 2 - "addToBackStack ()" luôn được sử dụng
Trường hợp 2A - thêm, xóa và nhấp vào nút Quay lại
Activity : onCreate() - onStart() - onResume() Activity is visible
add Fragment A : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment A is visible
add Fragment B : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment B is visible
add Fragment C : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
remove Fragment C : onPause() - onStop() - onDestroyView() Fragment B is visible
(Back button clicked)
Fragment C : onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
(Back button clicked)
Fragment C : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Fragment B is visible
(Back button clicked)
Fragment B : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Fragment A is visible
(Back button clicked)
Fragment A : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Activity is visible
(Back button clicked)
Activity : onPause() - onStop() - onDestroy() App is closed, nothing is visible
Trường hợp 2B - thêm, thay thế, xóa và nhấp vào nút Quay lại
Activity : onCreate() - onStart() - onResume() Activity is visible
add Fragment A : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment A is visible
add Fragment B : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment B is visible
(replace Fragment C)
Fragment B : onPause() - onStop() - onDestroyView()
Fragment A : onPause() - onStop() - onDestroyView()
Fragment C : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
remove Fragment C : onPause() - onStop() - onDestroyView() Activity is visible
(Back button clicked)
Fragment C : onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
(Back button clicked)
Fragment C : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
Fragment A : onCreateView() - onActivityCreated() - onStart() - onResume()
Fragment B : onCreateView() - onActivityCreated() - onStart() - onResume() Fragment B is visible
(Back button clicked)
Fragment B : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Fragment A is visible
(Back button clicked)
Fragment A : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Activity is visible
(Back button clicked)
Activity : onPause() - onStop() - onDestroy() App is closed, nothing is visible
Tùy chọn 3 - "addToBackStack ()" không được sử dụng luôn (trong các ví dụ dưới đây, w / o chỉ ra rằng nó không được sử dụng)
Trường hợp 3A - thêm, xóa và nhấp vào nút Quay lại
Activity : onCreate() - onStart() - onResume() Activity is visible
add Fragment A : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment A is visible
add Fragment B w/o: onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment B is visible
add Fragment C w/o: onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
remove Fragment C : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Fragment B is visible
(Back button clicked)
Fragment B : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
Fragment A : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Activity is visible
(Back button clicked)
Activity : onPause() - onStop() - onDestroy() App is closed, nothing is visible
Trường hợp 3B - thêm, thay thế, xóa và nhấp vào nút Quay lại
Activity : onCreate() - onStart() - onResume() Activity is visible
add Fragment A : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment A is visible
add Fragment B w/o: onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment B is visible
(replace Fragment C)
Fragment B : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
Fragment A : onPause() - onStop() - onDestroyView()
Fragment C : onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
remove Fragment C : onPause() - onStop() - onDestroyView() Activity is visible
(Back button clicked)
Fragment C : onCreateView() - onActivityCreated() - onStart() - onResume() Fragment C is visible
(Back button clicked)
Fragment C : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
Fragment A : onCreateView() - onActivityCreated() - onStart() - onResume() Fragment A is visible
(Back button clicked)
Fragment A : onPause() - onStop() - onDestroyView() - onDestroy() - onDetach() Activity is visible
(Back button clicked)
Activity : onPause() - onStop() - onDestroy() App is closed, nothing is visible