{"id":4888,"date":"2019-06-21T13:52:26","date_gmt":"2019-06-21T08:22:26","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4888"},"modified":"2026-11-13T12:15:04","modified_gmt":"2026-11-13T06:45:04","slug":"fundamental-things-to-know-in-android-activity-and-fragment","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/fundamental-things-to-know-in-android-activity-and-fragment\/","title":{"rendered":"Fundamental Things to Know in Android Activity and Fragment"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Post Date:<\/strong> June 12, 2019<br><strong>Last Updated:<\/strong> November 13, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Activities and Fragments are two fundamental components of Android application development. They help developers organize an application&#8217;s user interface, manage screen navigation, and handle user interactions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An <strong>Activity<\/strong> represents a screen or entry point into an Android application, while a <strong>Fragment<\/strong> represents a reusable portion of the user interface hosted within an Activity or another Fragment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding the differences between Activities and Fragments, their lifecycle methods, and how to navigate between them is essential when developing Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explore the basic concepts of Android Activities and Fragments, their lifecycle methods, and how to perform Fragment transactions using Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is an Activity in Android?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity\" target=\"_blank\" rel=\"noopener\">Activity<\/a> is a core Android application component that provides a window in which an application displays its user interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An Activity generally represents a single screen of an application. For example, a shopping application might contain separate screens for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Login<\/li>\n\n\n\n<li>Home<\/li>\n\n\n\n<li>Product details<\/li>\n\n\n\n<li>Shopping cart<\/li>\n\n\n\n<li>User profile<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Each screen can be implemented using an Activity, although modern Android applications often use a single-Activity architecture with multiple Fragments or other navigation destinations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An Activity works with a layout file to display its user interface. For example, an XML layout can define buttons, text fields, images, and other UI components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Declaring an Activity in AndroidManifest.xml<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before an Activity can be launched as part of your application, it must be declared in the application&#8217;s manifest.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\">\n\n    &lt;application\n        android:allowBackup=\"true\"\n        android:label=\"My Application\"\n        android:theme=\"@style\/Theme.MyApplication\">\n\n        &lt;activity\n            android:name=\".ExampleActivity\"\n            android:exported=\"false\" \/>\n\n    &lt;\/application>\n\n&lt;\/manifest><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>android:name<\/code> attribute specifies the Activity class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For an Activity that serves as the application&#8217;s launcher entry point, configure the appropriate <code>MAIN<\/code> action and <code>LAUNCHER<\/code> category intent filter. On Android 12 and later, components with intent filters must explicitly declare <code>android:exported<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating an Activity<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To create an Activity, define a class that extends an Android Activity class, such as <code>AppCompatActivity<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import android.os.Bundle;\nimport androidx.appcompat.app.AppCompatActivity;\n\npublic class ExampleActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_example);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onCreate()<\/code> method is called when the system creates the Activity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is commonly used to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initialize the Activity.<\/li>\n\n\n\n<li>Load the user interface.<\/li>\n\n\n\n<li>Initialize UI components.<\/li>\n\n\n\n<li>Set up event listeners.<\/li>\n\n\n\n<li>Restore previously saved state when available.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>setContentView()<\/code> method connects the Activity to its XML layout file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Navigating Between Activities Using Intent<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An Activity can launch another Activity using an <code>Intent<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, to open <code>SecondActivity<\/code> from <code>MainActivity<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Intent intent = new Intent(MainActivity.this, SecondActivity.class);\nstartActivity(intent);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An Intent describes an operation to perform. It can be used to launch another Activity, pass data between components, and interact with other Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To pass data to another Activity:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Intent intent = new Intent(MainActivity.this, SecondActivity.class);\n\nintent.putExtra(\"username\", \"John\");\n\nstartActivity(intent);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The receiving Activity can retrieve the value:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">String username = getIntent().getStringExtra(\"username\");<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Android Activity Lifecycle<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every Activity has its own lifecycle. The Android operating system manages this lifecycle by calling specific callback methods as the Activity changes its state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding these callbacks helps developers manage resources, handle interruptions, and maintain application state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The main Activity lifecycle methods are:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><code>onCreate()<\/code><\/li>\n\n\n\n<li><code>onStart()<\/code><\/li>\n\n\n\n<li><code>onResume()<\/code><\/li>\n\n\n\n<li><code>onPause()<\/code><\/li>\n\n\n\n<li><code>onStop()<\/code><\/li>\n\n\n\n<li><code>onRestart()<\/code><\/li>\n\n\n\n<li><code>onDestroy()<\/code><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Activity Lifecycle Methods<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. onCreate()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onCreate()<\/code> method is called when the Activity is first created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is used to initialize essential components, load the layout, and prepare the Activity.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>savedInstanceState<\/code> parameter contains previously saved instance state when available. It can be <code>null<\/code> when no saved state exists.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. onStart()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onStart()<\/code> method is called when the Activity becomes visible to the user.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It prepares the Activity to enter the foreground.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\nprotected void onStart() {\n    super.onStart();\n    \/\/ Prepare resources needed while the Activity is visible\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. onResume()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onResume()<\/code> method is called when the Activity enters the foreground and becomes ready for user interaction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is where developers can initialize functionality that should be active while the user interacts with the screen.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\nprotected void onResume() {\n    super.onResume();\n    \/\/ Resume UI-related operations\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. onPause()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onPause()<\/code> method is called when the Activity loses foreground focus.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This does not necessarily mean that the Activity is completely hidden. For example, a partially transparent Activity or a multi-window situation can cause the current Activity to become paused while remaining visible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is commonly used to pause operations that should not continue while the Activity is not in the foreground.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\nprotected void onPause() {\n    super.onPause();\n    \/\/ Pause foreground-only operations\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keep this callback lightweight. Avoid performing lengthy database operations or network requests here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5. onStop()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onStop()<\/code> method is called when the Activity is no longer visible to the user.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is useful for releasing resources and stopping operations that are unnecessary while the Activity is hidden.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\nprotected void onStop() {\n    super.onStop();\n    \/\/ Release resources not needed while the Activity is hidden\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6. onRestart()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onRestart()<\/code> method is called when a previously stopped Activity is about to become visible again.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is followed by <code>onStart()<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\nprotected void onRestart() {\n    super.onRestart();\n    \/\/ Prepare the Activity to become visible again\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>7. onDestroy()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onDestroy()<\/code> method is called before an Activity is destroyed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can happen when the Activity finishes or when the system recreates it due to a configuration change, such as device rotation.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\nprotected void onDestroy() {\n    super.onDestroy();\n    \/\/ Clean up remaining resources\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important:<\/strong> The Android system can terminate an application process without calling <code>onDestroy()<\/code>. Therefore, do not rely on this method as the only place to save important application data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Activity Lifecycle Flow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The simplified Activity lifecycle follows this sequence:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        onCreate()\n            |\n        onStart()\n            |\n        onResume()\n            |\n       User interacts\n            |\n        onPause()\n          \/    \\\n         \/      \\\n    onResume()  onStop()\n                  |\n              onRestart()\n                  |\n               onStart()\n                  |\n              onResume()\n\n        Or from onStop():\n\n             onDestroy()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding this lifecycle helps developers manage application resources and provide a consistent user experience.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. What Is a Fragment in Android?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <a href=\"https:\/\/developer.android.com\/guide\/fragments\" target=\"_blank\" rel=\"noopener\">Fragment<\/a> represents a reusable portion of an application&#8217;s user interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Fragment has its own lifecycle, manages its own UI, and can handle user interactions. However, unlike an Activity, a Fragment cannot exist independently. It must be hosted by an Activity or another Fragment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can think of a Fragment as a modular section of an Activity that can be added, removed, or replaced while the Activity is running.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, an application might use different Fragments for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Home screen content<\/li>\n\n\n\n<li>User profile<\/li>\n\n\n\n<li>Product details<\/li>\n\n\n\n<li>Settings<\/li>\n\n\n\n<li>Search results<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Fragments help developers create modular and reusable application interfaces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Fragment<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To create a Fragment, extend the AndroidX <code>Fragment<\/code> class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import android.os.Bundle;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\n\nimport androidx.annotation.NonNull;\nimport androidx.annotation.Nullable;\nimport androidx.fragment.app.Fragment;\n\npublic class ExampleFragment extends Fragment {\n\n    public ExampleFragment() {\n        super(R.layout.fragment_example);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>fragment_example.xml<\/code> file defines the user interface for this Fragment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\n&lt;LinearLayout\n    xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"\n    android:padding=\"16dp\">\n\n    &lt;TextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Welcome to Example Fragment\"\n        android:textSize=\"20sp\" \/>\n\n&lt;\/LinearLayout><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The Fragment class manages the layout and its lifecycle within the hosting Activity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Understanding the Fragment Lifecycle<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to an Activity, a Fragment has its own lifecycle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Some important Fragment lifecycle callbacks include:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Lifecycle method<\/th><th>Purpose<\/th><\/tr><tr><td><code>onAttach()<\/code><\/td><td>Called when the Fragment is attached to its host.<\/td><\/tr><tr><td><code>onCreate()<\/code><\/td><td>Initializes Fragment-level state.<\/td><\/tr><tr><td><code>onCreateView()<\/code><\/td><td>Creates the Fragment&#8217;s user interface.<\/td><\/tr><tr><td><code>onViewCreated()<\/code><\/td><td>Called after the Fragment&#8217;s view has been created.<\/td><\/tr><tr><td><code>onStart()<\/code><\/td><td>Makes the Fragment visible.<\/td><\/tr><tr><td><code>onResume()<\/code><\/td><td>Allows the Fragment to interact with the user.<\/td><\/tr><tr><td><code>onPause()<\/code><\/td><td>Called when the Fragment loses foreground interaction.<\/td><\/tr><tr><td><code>onStop()<\/code><\/td><td>Called when the Fragment is no longer visible.<\/td><\/tr><tr><td><code>onDestroyView()<\/code><\/td><td>Called when the Fragment&#8217;s view hierarchy is destroyed.<\/td><\/tr><tr><td><code>onDestroy()<\/code><\/td><td>Cleans up Fragment-level state.<\/td><\/tr><tr><td><code>onDetach()<\/code><\/td><td>Called when the Fragment is detached from its host.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Important: Fragment View Lifecycle<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Fragment&#8217;s lifecycle and its view lifecycle are separate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, when a Fragment is placed on the back stack, its view can be destroyed while the Fragment instance remains managed by the FragmentManager.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>onDestroyView()<\/code> to clean up references to views and view binding objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This distinction is important when managing UI resources and preventing memory leaks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. What Are Fragment Transactions?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Fragment transactions allow developers to add, remove, replace, show, or hide Fragments dynamically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, suppose an application has a <code>MainActivity<\/code> with a container that displays different Fragments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To navigate to another Fragment, we can use the <code>FragmentManager<\/code> and <code>FragmentTransaction<\/code> classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a Fragment Container<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, define a container in the Activity layout.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: <code>activity_main.xml<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\n&lt;FrameLayout\n    xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:id=\"@+id\/fragment_container\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\" \/><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create a New Fragment<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Fragment class named <code>ExampleFragment<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import androidx.fragment.app.Fragment;\n\npublic class ExampleFragment extends Fragment {\n\n    public ExampleFragment() {\n        super(R.layout.fragment_example);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Perform a Fragment Transaction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, use the <code>FragmentManager<\/code> to replace the existing Fragment with the new Fragment.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import android.os.Bundle;\n\nimport androidx.appcompat.app.AppCompatActivity;\nimport androidx.fragment.app.Fragment;\nimport androidx.fragment.app.FragmentManager;\nimport androidx.fragment.app.FragmentTransaction;\n\npublic class MainActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        if (savedInstanceState == null) {\n            Fragment newFragment = new ExampleFragment();\n\n            FragmentManager fragmentManager =\n                    getSupportFragmentManager();\n\n            FragmentTransaction transaction =\n                    fragmentManager.beginTransaction();\n\n            transaction.replace(\n                    R.id.fragment_container,\n                    newFragment\n            );\n\n            transaction.addToBackStack(null);\n            transaction.commit();\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>getSupportFragmentManager()<\/code> obtains the FragmentManager associated with the Activity.<\/li>\n\n\n\n<li><code>beginTransaction()<\/code> starts a new Fragment transaction.<\/li>\n\n\n\n<li><code>replace()<\/code> replaces the Fragment currently displayed in the specified container.<\/li>\n\n\n\n<li><code>addToBackStack(null)<\/code> adds the transaction to the back stack, allowing the user to return to the previous Fragment using the Back button.<\/li>\n\n\n\n<li><code>commit()<\/code> schedules the transaction to be executed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>savedInstanceState == null<\/code> condition prevents the initial Fragment from being added again when the Activity is recreated after a configuration change.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Difference Between Activity and Fragment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both Activities and Fragments are important Android components, but they serve different purposes.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Activity<\/th><th>Fragment<\/th><\/tr><tr><td>Represents an application screen or entry point.<\/td><td>Represents a reusable portion of the UI.<\/td><\/tr><tr><td>Can exist independently within the application.<\/td><td>Must be hosted by an Activity or another Fragment.<\/td><\/tr><tr><td>Declared in the Android manifest.<\/td><td>Managed through a FragmentManager.<\/td><\/tr><tr><td>Uses Activity lifecycle callbacks.<\/td><td>Has its own lifecycle and view lifecycle.<\/td><\/tr><tr><td>Can launch another Activity using an Intent.<\/td><td>Can be added, removed, or replaced using Fragment transactions.<\/td><\/tr><tr><td>Provides a window for the application UI.<\/td><td>Displays its UI within the host&#8217;s view hierarchy.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Activities and Fragments are fundamental concepts in Android application development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An Activity provides the application screen and manages its lifecycle, while a Fragment provides a modular and reusable section of the user interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding their lifecycle methods, navigation mechanisms, and Fragment transactions helps developers build maintainable Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Although Activities and Fragments can both be used to organize application screens, choosing the appropriate component depends on the application&#8217;s architecture and navigation requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the main difference between Activity and Fragment in Android?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An Activity represents an application screen or entry point, whereas a Fragment represents a reusable portion of the UI that must be hosted by an Activity or another Fragment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Can a Fragment exist without an Activity?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. A Fragment must be hosted by an Activity or another Fragment. It cannot operate independently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the purpose of onCreate() in Android Activity?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onCreate()<\/code> method initializes the Activity, loads its layout, and prepares the components required for the screen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. What is a Fragment transaction in Android?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Fragment transaction is a set of operations used to add, remove, replace, show, or hide Fragments within an application&#8217;s UI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. What is the difference between onDestroy() and onDestroyView()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>onDestroyView()<\/code> is called when a Fragment&#8217;s view hierarchy is destroyed. <code>onDestroy()<\/code> is called when the Fragment instance itself is being destroyed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.android.com\/guide\/components\/activities\/intro-activities\" target=\"_blank\" rel=\"noopener\">Android Developers \u2013 Activities<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/guide\/components\/activities\/activity-lifecycle\" target=\"_blank\" rel=\"noopener\">Android Developers \u2013 Activity Lifecycle<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/guide\/fragments\" target=\"_blank\" rel=\"noopener\">Android Developers \u2013 Fragments<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/guide\/fragments\/lifecycle\" target=\"_blank\" rel=\"noopener\">Android Developers \u2013 Fragment Lifecycle<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/guide\/fragments\/transactions\" target=\"_blank\" rel=\"noopener\">Android Developers \u2013 Fragment Transactions<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/firebase-in-android-studio\/\">Firebase in Android Studio<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-generate-apk-in-android-studio\/\">How to generate APK in Android Studio<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Need Assistance?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Need help with Android application development, Activity and Fragment implementation, or troubleshooting Android application issues?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/pheonixsolutions.com\/mobile-app-development\/\" data-type=\"link\" data-id=\"https:\/\/pheonixsolutions.com\/mobile-app-development\/\">Pheonix Solutions<\/a> technical team can assist you with Android development, application maintenance, and troubleshooting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For assistance, <a href=\"https:\/\/pheonixsolutions.com\/contact\/\" data-type=\"link\" data-id=\"https:\/\/pheonixsolutions.com\/contact\/\">contact us<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: June 12, 2019Last Updated: November 13, 2026 Introduction Activities and Fragments are two fundamental components of Android application [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13270,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1021],"tags":[569,577,681],"class_list":["post-4888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile-flutter","tag-android","tag-android-development","tag-fundamental","psol-cat-mobile-flutter"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1gQ","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/06\/Screenshot-2026-11-13-at-12.13.45-pm.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4888"}],"version-history":[{"count":2,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4888\/revisions"}],"predecessor-version":[{"id":13272,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4888\/revisions\/13272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/13270"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}