The Activity class provides a number of callbacks that allow the activity to know that a state has changed, that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.
LifeCycle’s methods sequence
onCreate() -> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy()
Fragment has its own lifecycle. When a user navigates and interacts with your app, your fragments transition through various states in their lifecycle as they are added, removed, and enter or exit the screen.
Let’s assume we have two fragments A and B. Fragment A is already attached with an activity and have to add fragment B on top of fragment A.
FragmentB fb=new FragmentB();
Fragment B’s lifecycle methods will call only, no method of fragment A.
onAttach() -> onCreate() -> onCreateView() -> onActivityCreated() -> onStart() -> onResume()
What happens when press back key button on above situation: Again…