Activity Vs Fragment

Yigitkaantonkaz
IBTech
Published in
4 min readSep 4, 2023

Hello everyone,

In this article, I will try to explain the concepts of Activity and Fragment in Android development and how their lifecycles work.

First of all, let’s talk about their definitions.

Activity is a section or a screen from which the application starts and allows the app’s user interface to be organized and arranged. On the other hand, a Fragment is a modular component of the user interface that is a part of an activity. Additionally, it is capable of handling input events and cannot exist without activity.

The application goes through some states while working on the activity or fragment. For instance, when the user is navigating the application, exiting the app, or running it in the background, it switches between different states. We can say that the lifecycles are the concept that shows these transitions between these states and explains the mechanism. Both Activity and Fragment have unique lifecycles of their own.

Firstly, for the Activity lifecycle, there are 7 main activity callbacks.

onCreate()

It is called when the activity is first created, so it’s only called once during the activity’s lifecycle.

onStart()

It is called when the activity is visible to the user. It can be called multiple times when the user left or paused and again launched the activity.

onResume()

When the Activity is in the onResume state, the application comes to the foreground. This method is called when the user is interacting with the activity.

onPause()

It is called when the user cannot interact with the activity. It is usually invoked when pressing the back or home button. Activity does not receive any user input and cannot run any code when it is on paused state.

onStop()

It is called when the activity is no longer visible to the user.

onDestroy()

It is called when the activity will be completely destroyed. The activity is destroyed when the back button is pressed or the finish() method is called.

onRestart()

This callback invoked when restarting an activity in a stopped state. After onRestart, the activity becomes visible with onStart.

Here is the lifecycle map of the activity,

Figure 1. A simplified illustration of the activity lifecycle.(https://developer.android.com/guide/components/activities/activity-lifecycle)

Below, we can check which stages are active in some scenarios.

→Application opening

onCreate() → onStart() → onResume()

->When pressed the home button

onPause() → onStop()

->When returning to the application after pressing the Home button

onRestart () → onStart() → onResume()

To talk about Fragments, they also have their own layout and lifecycle callbacks.

Figure 2. Fragment Lifecycle states and their relation to both the fragment’s lifecycle callbacks and the fragment’s view Lifecycle.

Another feature of fragments is that there can be multiple fragments in one activity and one fragment can be used in many different activities.

These are the callbacks of fragment,

onAttach(Activity)

It is called only once when it is connected to activity.

onCreate(Bundle)

It is called when initialize the fragment

onCreateView(LayoutInflater, ViewGroup, Bundle)

It creates and returns the view hierarchy.

onActivityCreated(Bundle)

It is invoked when the onCreate() method of the host activity has completed.

onStart()

It is called to make the fragment is visible on screen.

onResume()

It is called to make the fragment active.

onPause()

It is called when the user cannot interact with the fragment.

onStop()

It is called when the fragment is no longer visible to the user.

onDestroyView()

Invoked when the fragment’s view is ended, but the fragment still maintained. In other words, it allows fragment to clean up.

onDestroy()

Invoked when the fragment is no longer in usage.

onDetach()

It is called when the fragment is no longer associated with its activity.

And Finally to sum up, the map below can help to understand the concepts of Activity and Fragment in Kotlin.

Figure 3. Fragment Lifecycle.

Sources

--

--