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()

Activity Lifecycle Diagram

Lifecycle callbacks when one activity comes on top of another

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.

Fragment Life Cycle Diagram

Fragment callbacks while adding a fragment to another

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…

Sohan Gupta

Android | Java | Kotlin | Full Stack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store