Understanding Android Lifecycle – Activity and Fragment Lifecycle - Textnotes

Understanding Android Lifecycle – Activity and Fragment Lifecycle


Learn about the Activity and Fragment lifecycle in Android. Understand key methods like onCreate(), onStart(), onResume(), and Fragment lifecycle methods like onAttach(), onCreateView(), and more.

In Android, managing the lifecycle of components such as Activities and Fragments is a critical part of app development. The lifecycle methods help manage the app’s behavior based on various system events, such as backgrounding, user interaction, or configuration changes. Understanding these lifecycle methods ensures your app is efficient and responsive.

i) Activity Lifecycle

An Activity is a single screen in an Android application, and it goes through a series of lifecycle stages. The system uses lifecycle methods to transition through these stages, and as a developer, understanding these methods will help manage resources, data, and UI updates efficiently.

Key Activity Lifecycle Methods

a) onCreate()

This method is called when the activity is first created. It is used to initialize components, set the layout, and perform one-time setup actions.


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

b) onStart()

Called when the activity is becoming visible to the user. This method is invoked after onCreate() and before onResume().


@Override
protected void onStart() {
super.onStart();
}

c) onResume()

Called when the activity is in the foreground and interacting with the user. It is used to restart tasks like animations, refresh data, or any ongoing processes.


@Override
protected void onResume() {
super.onResume();
}

d) onPause()

This method is called when the system is about to start or resume another activity. It’s used to pause ongoing tasks, release resources, and save any necessary data.


@Override
protected void onPause() {
super.onPause();
}

e) onStop()

Called when the activity is no longer visible to the user. This is a good place to release resources that are no longer needed.


@Override
protected void onStop() {
super.onStop();
}

f) onRestart()

This method is invoked when the activity is restarted after being stopped. It is called just before onStart().


@Override
protected void onRestart() {
super.onRestart();
}

g) onDestroy()

Called when the activity is about to be destroyed. This is where you should clean up any resources and stop any background tasks.


@Override
protected void onDestroy() {
super.onDestroy();
}

Activity Lifecycle Flow

i) onCreate() → onStart() → onResume()

When the activity is paused or stopped:

ii) onPause() → onStop()

When the activity is restarted:

iii) onRestart() → onStart() → onResume()

Finally, when the activity is destroyed:

iv) onDestroy()

ii) Fragment Lifecycle

A Fragment is a portion of the activity’s UI or behavior. Since fragments can be dynamically added or removed during runtime, they have their own lifecycle, which is managed by the hosting activity.

Key Fragment Lifecycle Methods

a) onAttach()

This method is called when the fragment is first attached to its host activity. Here you can access the activity's context and perform initial setup.


@Override
public void onAttach(Context context) {
super.onAttach(context);
}

b) onCreate()

Called to initialize the fragment. This method is used for setting up configurations that do not depend on the fragment’s view.


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

c) onCreateView()

This method is called to create the fragment’s view. The view is inflated from the layout resource and is used as the UI of the fragment.


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_example, container, false);
}

d) onActivityCreated()

Called after the host activity’s onCreate() method has been invoked. You can access and interact with the activity’s views here.


@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

e) onStart()

This method is invoked when the fragment becomes visible to the user. It is called after onCreateView() and before onResume().


@Override
public void onStart() {
super.onStart();
}

f) onResume()

This method is called when the fragment is active and in the foreground. Use this to start tasks that need to run while the fragment is visible and interacting with the user.


@Override
public void onResume() {
super.onResume();
}

g) onPause()

Called when the fragment is no longer in the foreground but still partially visible. This method is used to pause ongoing tasks and save data if necessary.


@Override
public void onPause() {
super.onPause();
}

h) onStop()

This method is called when the fragment is no longer visible to the user. It is similar to the onStop() method in activities.


@Override
public void onStop() {
super.onStop();
}

i) onDestroyView()

This method is called when the fragment’s view is being destroyed. Here, you can clean up resources that were tied to the fragment’s view.


@Override
public void onDestroyView() {
super.onDestroyView();
}

j) onDestroy()

Called when the fragment is being destroyed. This is the time to clean up any remaining resources.


@Override
public void onDestroy() {
super.onDestroy();
}

k) onDetach()

This is the final method in the fragment lifecycle, called when the fragment is detached from its host activity.


@Override
public void onDetach() {
super.onDetach();
}

Fragment Lifecycle Flow

i) onAttach() → onCreate() → onCreateView() → onActivityCreated() → onStart() → onResume()

When the fragment is paused or stopped:

ii) onPause() → onStop() → onDestroyView() → onDestroy()

Finally, when the fragment is detached:

iii) onDetach()

iii) Key Differences Between Activity and Fragment Lifecycles

MethodActivityFragment
onCreate()Called when the activity is createdCalled when the fragment is created
onCreateView()Not applicableCalled to create the fragment's view
onDestroyView()Not applicableCalled to destroy the fragment's view
onPause()Called when the activity is pausedCalled when the fragment is paused
onStop()Called when the activity is stoppedCalled when the fragment is stopped