Junior — Middle
What lifecycle methods of an activity are involved when switching from one fragment to another?
sobes.tech AI
Answer from AI
When switching from one fragment to another in Android, the activity's lifecycle usually involves the following methods:
- onPause() — called when the activity is partially covered by a new fragment or another activity.
- onStop() — may be called if the activity is fully hidden.
However, when replacing a fragment within an activity, the activity itself is usually not destroyed or recreated, so methods like onCreate() or onDestroy() are not called.
The lifecycle mainly affects the fragments themselves:
- The old fragment goes through onPause(), onStop(), onDestroyView(), and possibly onDestroy().
- The new fragment goes through onCreateView(), onStart(), onResume().
Thus, switching fragments affects the lifecycle of the fragments themselves, while the activity only partially (onPause/onStop), if it is not completely hidden.