Activity & Fragment in Android

Deepak Kaligotla
6 min readSep 2, 2023

--

Activity Life Cycle

In Android, an activity is a component of an app that represents a single screen with a user interface. The activity lifecycle refers to the different states an activity can be in, and the events that can occur during those states. The activity lifecycle consists of the following states:

  1. Created: The activity has been created but not yet started.
  2. Started: The activity is visible but not yet interactive.
  3. Resumed: The activity is visible and interactive.
  4. Paused: Another activity is in the foreground, but the activity is still visible.
  5. Stopped: The activity is no longer visible.
  6. Destroyed: The activity has been destroyed and is no longer available.

Here are some examples of how an activity might transition between these states:

  1. Created: When an activity is first created, the onCreate() method is called. This is where you would typically set up the UI and any data structures needed for the activity. For example, if you’re creating a simple activity that displays a message, you might use the onCreate() method to set the message text.
  2. Started: Once the activity has been created, it moves to the started state. The onStart() method is called at this point. This is where you might start any background threads or load any data that your activity needs. For example, if you’re building an activity that displays a list of items, you might load the list of items in the onStart() method.
  3. Resumed: When the activity is ready to be displayed, it moves to the resumed state. The onResume() method is called at this point. This is where you would typically start any animations or audio that your activity needs. For example, if you’re building a game, you might start the game loop in the onResume() method.
  4. Paused: If the activity is partially obscured by another activity, it moves to the paused state. The onPause() method is called at this point. This is where you might stop any animations or audio that your activity was playing. For example, if you’re building a music player, you might pause the music playback in the onPause() method.
  5. Stopped: If the activity is no longer visible, it moves to the stopped state. The onStop() method is called at this point. This is where you might release any resources that your activity was using. For example, if you’re building a camera app, you might release the camera resources in the onStop() method.
  6. Destroyed: Finally, when the activity is no longer needed, it moves to the destroyed state. The onDestroy() method is called at this point. This is where you might release any remaining resources that your activity was using. For example, if you’re building a location-based app, you might stop the location updates in the onDestroy() method.

Fragment Life Cycle

In Android, a Fragment represents a portion of a user interface or behaviour in an Activity. Fragments have their own lifecycle, which is closely tied to the lifecycle of the Activity they are a part of. Here are the different states in the Fragment lifecycle:

  1. Instantiated — When a Fragment object is first created with the new keyword or through a FragmentManager, it is in an instantiated state.
  2. Attached — Once a Fragment is associated with an Activity using the FragmentTransaction.add() or FragmentTransaction.replace() method, it becomes attached to that Activity.
  3. Created — When a Fragment is attached to an Activity, it goes through its onCreate() method. This is where you should initialize your Fragment.
  4. Create View — After the onCreate() method is called, the Fragment’s onCreateView() method is called. This is where you should inflate your layout and set up any views.
  5. View Created — After the onCreateView() method is called, the Fragment’s onViewCreated() method is called. This is where the view is created and ready.
  6. View State Restored — After onViewCreated() method is called, the Fragment check for any state is stored to restore in onViewStateRestored() method.
  7. Started — Once the Fragment’s view is created, it goes through its onStart() method. This means that the Fragment is now visible to the user.
  8. Resumed — When the Fragment is in the foreground of the Activity, it goes through its onResume() method. This is where you should start any animations or other ongoing actions that should be paused or resumed when the Fragment is in the foreground or background.
  9. Paused — When the user navigates away from the Fragment, it goes through its onPause() method. This is where you should stop any ongoing actions that should be paused when the Fragment is not visible.
  10. Stopped — When the Fragment is no longer visible to the user, it goes through its onStop() method. This is where you should release any resources that are no longer needed.
  11. Save Instance State — after the application went behind means it is not on the foreground and onPause(), onStop() it checks if onSaveInstanceState() to save the state which will be restore in onViewStateRestored() method after onViewCreated() method.
  12. Destroyed View — If the Fragment is removed from the Activity, it goes through its onDestroyView() method. This is where you should clean up any references to views that were created in the onCreateView() method.
  13. Destroyed — Finally, when the Fragment is no longer needed, it goes through its onDestroy() method. This is where you should clean up any resources that are no longer needed.
  14. Detached — Once a Fragment is closed with an Activity using, it is detached from the activity. When Fragment is replaced or moved from one fragment to another, current fragment is detached from the activity.

What is the difference between Activity and Fragment in Android?

  1. Fragment have more lifecycle methods — Instantiated, onAttach, onCreateView, onViewCreated, onRestoreInstanceState, onSavedInstanceState, onDestroyedView, onDetach
  2. We cannot create a fragment with out an activity, as the fragment needs to be instantiated inside the activity.
  3. Multiple fragments can be created in an activity, such as Landscape_fragment, portrait_fragment. Which calls respective fragment UI to user depending on the screen orientation.
  4. Fragment is used mainly for Bottom Navigation — HomeActivity or MainActivity can contain multiple screens like Login / Register / List / … / Settings where these screens can be fragments and the main screen will be Activity.

Different ways to pass data between Activities and Fragments

  1. Activity to Activity:

Intent with extras: You can pass data between activities using intents with extras. You can put extras in the intent object and retrieve them in the target activity using getIntent().getExtras().

Parcelable or Serializable: You can also make your custom data model implement the Parcelable or Serializable interface and pass it in the intent as an extra.

Static Variables: You can also use static variables to pass data between activities, but this is not recommended as it can lead to memory leaks and other issues.

2. Fragment to Fragment:

Bundle: You can pass data between fragments using a Bundle. You can create a Bundle object and set data in it, then set the Bundle as the arguments for the target fragment using setArguments() method.

ViewModel: You can also use a shared ViewModel to share data between fragments.

3. Fragment to Activity:

Interface: You can define an interface in the Fragment and implement it in the Activity. The Fragment can then call the interface method to pass data to the Activity.

Intent with extras: You can also pass data from a Fragment to an Activity using an intent with extras.

4. Activity to Fragment:

Interface: You can define an interface in the Activity and implement it in the Fragment. The Activity can then call the interface method to pass data to the Fragment.

Bundle: You can pass data from an Activity to a Fragment using a Bundle. You can create a Bundle object and set data in it, then set the Bundle as the arguments for the target fragment using setArguments() method.

ViewModel: You can also use a shared ViewModel to share data between the Activity and Fragment.

--

--

Deepak Kaligotla
Deepak Kaligotla

Written by Deepak Kaligotla

I’m jobless, have 5.5 yrs experience in IT Tech support/helpdesk. Love learning and developing Mobile applications (Android & iOs). Contact me if you can hire