<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dilip Patel </title>
    <description>The latest articles on DEV Community by Dilip Patel  (@dilip2882).</description>
    <link>https://dev.to/dilip2882</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F572053%2F917ded6c-1b24-474b-80ee-905684e753e0.png</url>
      <title>DEV Community: Dilip Patel </title>
      <link>https://dev.to/dilip2882</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dilip2882"/>
    <language>en</language>
    <item>
      <title>Understanding Android Application Lifecycle and Process</title>
      <dc:creator>Dilip Patel </dc:creator>
      <pubDate>Wed, 26 Jun 2024 07:54:33 +0000</pubDate>
      <link>https://dev.to/dilip2882/understanding-android-application-lifecycle-and-process-3k50</link>
      <guid>https://dev.to/dilip2882/understanding-android-application-lifecycle-and-process-3k50</guid>
      <description>&lt;p&gt;Let’s dive into the details of how Android manages processes, components, and the overall lifecycle of an app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Understanding how Android manages processes, components, and the overall lifecycle of an application is fundamental for any developer creating  apps. This article delves into the core concepts behind process creation, control, and sandboxing within the Android system. It also explores the lifecycles of key components like Activities, Services, and BroadcastReceivers, providing a roadmap for effective component management. &lt;/p&gt;

&lt;h2&gt;
  
  
  Processes in Android
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Process Creation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every Android application runs in its own Linux process.&lt;/li&gt;
&lt;li&gt;When an app's code needs to run (e.g., launching an activity), the system creates a process for that app.&lt;/li&gt;
&lt;li&gt;The process remains running until the system reclaims its memory for other applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Process Lifetime Control&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unlike traditional systems, an app's process lifetime isn't directly controlled by the app itself.&lt;/li&gt;
&lt;li&gt;The system determines when to start, pause, or terminate a process based on various factors:

&lt;ul&gt;
&lt;li&gt;Active components (e.g., Activity, Service, BroadcastReceiver)&lt;/li&gt;
&lt;li&gt;Importance to the user&lt;/li&gt;
&lt;li&gt;Overall system memory availability&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Common Pitfall&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Incorrectly managing components (e.g., starting threads from a BroadcastReceiver) can lead to process termination.&lt;/li&gt;
&lt;li&gt;Solution: Use JobService to ensure active work is recognized by the system.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Sandboxing in Android Processes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Android enforces strong process isolation through sandboxing.&lt;/li&gt;
&lt;li&gt;Each app runs in its own sandboxed process, isolated from other apps.&lt;/li&gt;
&lt;li&gt;Linux user-based security ensures file permissions and access control.&lt;/li&gt;
&lt;li&gt;Inter-process communication (IPC) mechanisms allow controlled data exchange.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Viewing Android Processes with &lt;code&gt;adb shell ps&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;ADB (Android Debug Bridge) is a versatile command-line tool that allows you to interact with an Android device or emulator. One of its essential features is the ability to view running processes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before we begin, ensure that you have the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A computer with ADB installed.&lt;/li&gt;
&lt;li&gt;An Android device connected to your computer via USB.&lt;/li&gt;
&lt;li&gt;USB debugging enabled on your Android device (you can enable it in the Developer Options).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Steps to View Processes
&lt;/h3&gt;

&lt;p&gt;Follow these steps to view the processes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Connect your Android device to your computer via USB.&lt;/li&gt;
&lt;li&gt;Execute the following command:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adb shell ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will display a list of all processes, including their PIDs, UIDs, and other relevant details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycact6vlv5cwaknz3zhn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycact6vlv5cwaknz3zhn.png" alt="Image description" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the Output
&lt;/h3&gt;

&lt;p&gt;The output will include information such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PID (Process ID):&lt;/strong&gt; A unique identifier for each running process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UID (User ID):&lt;/strong&gt; The user associated with the process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Name:&lt;/strong&gt; The name of the process (e.g., app package name).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Status:&lt;/strong&gt; Whether the process is running, sleeping, or stopped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory usage:&lt;/strong&gt; Details about memory consumption.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Additional Tips
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To filter the output for a specific package name (e.g., &lt;code&gt;com.example.app&lt;/code&gt;), you can use:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adb shell ps | grep com.example.app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Some processes may run as system or root users, so their UIDs may differ.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;if it's showing grep not found, you can use below command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adb shell pgrep com.dilip.workmangagerexample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Application Object&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Application&lt;/code&gt; class (or application object) is a specific class within the Android framework.&lt;/li&gt;
&lt;li&gt;A single instance of this class is created when the application is launched.&lt;/li&gt;
&lt;li&gt;It remains alive for as long as the application is running.&lt;/li&gt;
&lt;li&gt;Developers can override the &lt;code&gt;onCreate()&lt;/code&gt; method in the application class to initialize global resources.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Global Objects&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global objects are referenced from the application object itself.&lt;/li&gt;
&lt;li&gt;They remain in memory for the entire lifetime of the application.&lt;/li&gt;
&lt;li&gt;Use global objects to share state between different components within your app.&lt;/li&gt;
&lt;li&gt;Avoid using singletons; prefer global objects for better resource management.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Understanding Component Lifecycles
&lt;/h1&gt;

&lt;p&gt;A well-structured app relies on a clear understanding of component lifecycles. Let's break down the key components and their lifecycle methods:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Activities&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These are the single screens that make up your app's UI.&lt;/li&gt;
&lt;li&gt;Lifecycle methods (onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy()) manage how an activity behaves at different stages:

&lt;ul&gt;
&lt;li&gt;Creation&lt;/li&gt;
&lt;li&gt;Visibility changes&lt;/li&gt;
&lt;li&gt;Pausing due to another activity coming to the foreground&lt;/li&gt;
&lt;li&gt;Destruction&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Services&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Designed for long-running operations or tasks that run in the background.&lt;/li&gt;
&lt;li&gt;Lifecycle methods (onCreate(), onStartCommand(), onDestroy()) govern their behavior.&lt;/li&gt;
&lt;li&gt;Different service types (bound, background, and foreground) address particular use cases.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;BroadcastReceivers&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These components respond to system-wide events or broadcasts.&lt;/li&gt;
&lt;li&gt;Simple lifecycle (onReceive()) executes only when a relevant broadcast is received.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Strategies for Process Optimization and Efficient App Management
&lt;/h2&gt;

&lt;p&gt;Now that we understand the core concepts, let's explore strategies to optimize processes and create efficient apps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Choose the Right Component for the Job&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Utilize lightweight components like BroadcastReceivers for short tasks.&lt;/li&gt;
&lt;li&gt;Use Services with appropriate start modes (foreground for critical tasks with user notification) for long-running operations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Asynchronous Operations&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Long-running tasks that block the UI thread can lead to a sluggish app.&lt;/li&gt;
&lt;li&gt;Techniques like AsyncTask or WorkManager help execute these tasks asynchronously, keeping your UI responsive.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Handling Configuration Changes&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Android can handles configuration changes like (e.g., screen orientation, language settings).&lt;/li&gt;
&lt;li&gt;onSaveInstanceState() and onRestoreInstanceState() methods allow activities to save and restore their state during such changes.&lt;/li&gt;
&lt;li&gt;ViewModel simplifies UI data management across configuration changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Background Execution with Control&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Carefully manage background tasks.&lt;/li&gt;
&lt;li&gt;Explore alternatives like JobScheduler or WorkManager for background operations.&lt;/li&gt;
&lt;li&gt;These offer more control and flexibility over execution timing compared to traditional Services.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Leverage Profiling Tools&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Android Studio's Profiler helps monitor app performance and identify potential memory leaks.&lt;/li&gt;
&lt;li&gt;Pinpoint resource bottlenecks to optimize your app's process usage and ensure a smooth user experience.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Out-of-Memory Killer (OOM Killer)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To manage limited system resources, the Android system can terminate running applications.&lt;/li&gt;
&lt;li&gt;Each application is started in a new process with a unique ID under a unique user.&lt;/li&gt;
&lt;li&gt;The system follows a priority system to determine which processes to terminate:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the Android system needs to terminate processes, it follows the following priority system:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Process Status&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Priority&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Foreground&lt;/td&gt;
&lt;td&gt;An application in which the user is interacting with an activity, or which has a service bound to such an activity. Also, if a service is executing one of its lifecycle methods or a broadcast receiver runs its &lt;code&gt;onReceive()&lt;/code&gt; method.&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Visible&lt;/td&gt;
&lt;td&gt;User is not interacting with the activity, but the activity is still (partially) visible, or the application has a service used by an inactive but visible activity.&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Service&lt;/td&gt;
&lt;td&gt;Application with a running service that does not qualify for 1 or 2.&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Background&lt;/td&gt;
&lt;td&gt;Application with only stopped activities and without a service or executing receiver. Android keeps them in a least recently used (LRU) list and terminates the one that was least used when necessary.&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty&lt;/td&gt;
&lt;td&gt;Application without any active components.&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The system maintains a least recently used (LRU) list of processes.&lt;/li&gt;
&lt;li&gt;The out-of-memory killer (OOM killer) terminates processes from the beginning of the LRU list.&lt;/li&gt;
&lt;li&gt;If an app is restarted by the user, it moves to the end of the queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Android applications run in their own Linux processes, with the system controlling their lifetime based on factors like active components, user importance, and memory availability. Sandboxing ensures app isolation and security. Activities, services, and broadcast receivers are the key components with their own lifecycles that need to be managed effectively. To optimize processes and create efficient apps, developers should choose the right component for the job, leverage asynchronous operations, handle configuration changes gracefully, manage background execution with control, and utilize profiling tools to identify potential issues. Finally, we discussed the Out-of-Memory (OOM) Killer, the system's mechanism for terminating processes when resources are limited, and the priority hierarchy it follows when making these decisions. By effectively managing processes and components, developers can create Android applications that are both user-friendly and resource-conscious. This article provides a solid foundation for understanding the intricacies of the Android system, allowing developers to optimize their apps for performance and efficiency.&lt;/p&gt;

</description>
      <category>android</category>
      <category>androiddev</category>
      <category>mobile</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Tasks, Back Stack &amp; Launch Modes in Android</title>
      <dc:creator>Dilip Patel </dc:creator>
      <pubDate>Wed, 19 Jun 2024 10:16:56 +0000</pubDate>
      <link>https://dev.to/dilip2882/tasks-back-stack-launch-modes-in-android-4mfl</link>
      <guid>https://dev.to/dilip2882/tasks-back-stack-launch-modes-in-android-4mfl</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fge7wjoqecx9t1jxshtuv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fge7wjoqecx9t1jxshtuv.png" alt="source: Android Developers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An activity is a screen shown in response to a user's action. For example, when you touch an app icon and it launches a screen, this first screen is the first activity. Technically speaking, an activity implements a part of the application’s visual user interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Activity States
&lt;/h2&gt;

&lt;p&gt;An activity can have four states and we will discuss them briefly here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Active&lt;/strong&gt;&lt;br&gt;
Activity is in an active state when it has the focus and it is ready to receive input from the user. The input can be in any form, most often a tap of a link or a button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inactive&lt;/strong&gt;&lt;br&gt;
When a user interacts with an activity or a screen, it usually causes a new activity to start and the system transfers the focus, or intent, to the new activity leaving the previous one inactive. An inactive activity is still there but in an inactive state. An inactive activity can receive focus and become active again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Destroyed&lt;/strong&gt;&lt;br&gt;
An activity is destroyed when a user presses the back button. This action destroys the active activity and passes the focus to the previous one. Once an activity is destroyed, it must be created again; there is no way to bring a destroyed activity to life.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hidden&lt;/strong&gt;&lt;br&gt;
An activity can remain active even when it is not visible to the user. This activity state is useful in certain multitasking scenarios.&lt;/p&gt;

&lt;p&gt;All activities must be represented by the element in the manifest file or they will never run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tasks
&lt;/h2&gt;

&lt;p&gt;A task is a collection of activities. Imagine a series of steps a user takes to complete a specific goal within your app. This sequence of activities, working together towards a common objective, forms a task. Tasks provide structure and maintain context for the user's journey through your app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Same Task, Shared Workflow&lt;/strong&gt;: Activities belonging to the same task share a common thread. They are part of a unified workflow, and their order of execution is crucial. These activities are stacked together within a single task, adhering to a Last-In, First-Out (LIFO) principle. The last activity added (launched) becomes the topmost activity in the task. When the user navigates back using the back button, the topmost activity is removed from the stack, revealing the previous activity underneath.&lt;/p&gt;

&lt;p&gt;Example: Consider a photo editing task. You might have activities for opening an image, applying filters, and saving the edited image. These activities would all belong to the same task, with the back button allowing users to navigate back through the editing process.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Example Kotlin code for starting an activity within the same task&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;intent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;EditImageActivity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Different Tasks, Independent Functionalities&lt;/strong&gt;: Not all activities are created equal. Some tasks might be entirely independent of others. For instance, an email app and a music player would likely function as separate tasks. Each task maintains its own independent stack of activities, and activities within different tasks cannot directly influence each other’s behavior.&lt;/p&gt;

&lt;p&gt;Example: Imagine you’re checking your email while listening to music. These actions represent two distinct tasks. Pressing the back button within the email app wouldn’t take you back to the music player, and vice versa.&lt;/p&gt;

&lt;h2&gt;
  
  
  Back Stack
&lt;/h2&gt;

&lt;p&gt;As users navigate through your app, launching new activities, a separate entity called the back stack keeps track of their journey. The back stack functions like a stack of plates, with each plate representing an activity. As new activities are launched, they’re added on top of the back stack. Conversely, pressing the back button removes the topmost activity (plate) from the back stack, revealing the previously viewed activity (plate) underneath.&lt;/p&gt;

&lt;h2&gt;
  
  
  Managing Tasks
&lt;/h2&gt;

&lt;p&gt;Android’s default task and back stack management is suitable for most applications, but there are times when developers need more control over their app’s navigation flow. This article explores how to manipulate task behavior using  manifest attributes and intent flags.&lt;/p&gt;

&lt;p&gt;Default Task Behavior: By default, Android places all activities started in succession within the same task, forming a “last in, first out” stack. This intuitive management allows users to navigate backward through the activity stack seamlessly.&lt;/p&gt;

&lt;p&gt;Interrupting Normal Behavior: There are scenarios where the default behavior might not align with the desired user experience. For instance, you might want an activity to start a new task or bring an existing instance of an activity to the foreground. Android provides several ways to achieve this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;taskAffinity: Defines the relationship between different tasks and activities. Activities with the same affinity are generally grouped together in the same task.&lt;/li&gt;
&lt;li&gt;launchMode: Specifies how an activity should be launched within a task. It can be “standard,” “singleTop,” “singleTask,” or “singleInstance.”&lt;/li&gt;
&lt;li&gt;allowTaskReparenting: If set to true, allows an activity to move from its task to the task it has an affinity with when that task comes to the foreground.&lt;/li&gt;
&lt;li&gt;clearTaskOnLaunch: When set to true, every time the user leaves the task and returns to it, all activities except for the root activity are finished.&lt;/li&gt;
&lt;li&gt;alwaysRetainTaskState: If true, the task retains its state and is not reset when launched from the home screen.&lt;/li&gt;
&lt;li&gt;finishOnTaskLaunch: Similar to clearTaskOnLaunch, but it also finishes the root activity, effectively clearing the entire task upon launch.&lt;/li&gt;
&lt;li&gt;Using Intent Flags: In addition to manifest attributes, developers can use intent flags such as FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP to control how activities are launched and managed within a task.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example Scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Starting a new task for an activity that serves as a distinct workflow within your app.&lt;/li&gt;
&lt;li&gt;Bringing an existing instance of an activity to the foreground instead of creating a new one.&lt;/li&gt;
&lt;li&gt;Clearing all activities except for the root when leaving a task.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Defining Launch Modes
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Using the manifest file
When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts.&lt;/li&gt;
&lt;li&gt;Using Intent flags
When you call startActivity(), you can include a flag in the Intent that declares how (or whether) the new activity should associate with the current task.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Activity Launch Modes: Controlling Activity Creation
&lt;/h2&gt;

&lt;p&gt;Now that we understand tasks and the back stack, let’s delve into launch modes. These modes dictate how activities are created and managed within the task hierarchy:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Launch Modes&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;standard&lt;/li&gt;
&lt;li&gt;singleTop&lt;/li&gt;
&lt;li&gt;singleTask&lt;/li&gt;
&lt;li&gt;singleInstance&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Standard (default)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x7af70bm6sh1ht8v8al.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x7af70bm6sh1ht8v8al.png" alt="Standard "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The default mode. The system creates a new instance of the activity in the task it was started from and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The most common launch mode. Whenever an activity is launched with the standard mode (or no mode is explicitly specified), a new instance of the activity is created. This new instance is placed on top of the current task’s stack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example: Imagine you have activities A, B, C, and D in your app. Launching B from activity A using the standard mode would create a new instance of B and add it on top of the stack, resulting in A -&amp;gt; B. Launching B again from the existing instance of B would create another new instance, forming A -&amp;gt; B -&amp;gt; B.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Launches a new instance of an activity in the task.&lt;/li&gt;
&lt;li&gt;Can create multiple instances of the same activity, assigned to the same or separate tasks.&lt;/li&gt;
&lt;li&gt;Launches a new instance of an activity in the task.&lt;/li&gt;
&lt;li&gt;Can create multiple instances of the same activity, assigned to the same or separate tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Launching B again results in: &lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; B&lt;/code&gt; (B is created again)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Example Kotlin code for standard launch mode&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;intent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ActivityB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;Single Top&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioqq4itabtz6de7y9kf4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioqq4itabtz6de7y9kf4.png" alt="Single Top"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its &lt;code&gt;onNewIntent()&lt;/code&gt; method, rather than creating a new instance of the activity. The activity is instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This mode offers more control over activity creation. If an activity is declared with &lt;code&gt;launchMode=“singleTop”&lt;/code&gt; in the manifest file, the following behavior occurs:&lt;/p&gt;

&lt;p&gt;If an instance of the activity already exists at the top of the stack within the current task, a new instance won’t be created. Instead, the existing activity receives the intent that triggered the launch through its &lt;code&gt;onNewIntent()&lt;/code&gt; method. This method allows you to handle updates within the existing activity instance.&lt;/p&gt;

&lt;p&gt;If the target activity with singleTop mode resides lower in the stack (not on top), a new instance is still created as usual.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example: Let’s say activities A, B (with singleTop mode), C, and D are present. Launching D from C creates a new instance (A -&amp;gt; B -&amp;gt; C -&amp;gt; D). Now, launching B again from D would not create a new instance of B if it’s already at the top of the stack; instead, B’s onNewIntent() is called.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Will not create a new activity if it’s already at the top of the stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Launching D again results in: &lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D&lt;/code&gt; (D is not created again)&lt;/li&gt;
&lt;li&gt;Launching B results in: &lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; B&lt;/code&gt; (B is created again because it’s not at the top)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Single Top launch mode example&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;singleTopIntent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SingleTopActivity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;singleTopIntent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_SINGLE_TOP&lt;/span&gt;
&lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;singleTopIntent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// In SingleTopActivity:&lt;/span&gt;
&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onNewIntent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onNewIntent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// Handle the new intent here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".ActivityB"&lt;/span&gt;
    &lt;span class="na"&gt;android:launchMode=&lt;/span&gt;&lt;span class="s"&gt;"singleTop"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/activity&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Single Top If an activity is already at the top of the stack, it will not be launched again. Instead, the onNewIntent() method will be called with the new intent.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Single Task&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The system creates the activity at the root of a new task or locates the activity on an existing task with the same affinity. If an instance of the activity already exists, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Meanwhile all of the other activities on top of it are destroyed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The singleTask launch mode ensures that only one instance of the activity exists in a task. If you launch an activity with singleTask mode, and it already exists in the current task (potentially with other activities on top of it in the stack), those previous activities will be destroyed, and the existing activity will receive the intent through its onNewIntent() method. This effectively creates a new single-activity task if the activity wasn't already running, or brings the existing task to the foreground if it was running in the background.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example: Consider activities A, B (with singleTask mode), C, and D. Launching B from A creates a new task with B at the root (B). Launching C from B adds C on top of the stack (B -&amp;gt; C). Now, launching A again from C wouldn't create a new instance of A or add it to the current task. Instead, the existing task containing B would be brought to the foreground, destroying C on top, and B would likely receive the intent through onNewIntent() (B).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Destroys any activities on top of the said activity if it’s already in the stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;A -&amp;gt; B -&amp;gt; C&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Launching D results in: &lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D&lt;/code&gt; (D is created)&lt;/li&gt;
&lt;li&gt;Launching B after D results in: &lt;code&gt;A -&amp;gt; B&lt;/code&gt; (B is brought to top, others are destroyed)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Single Task launch mode example&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;singleTaskIntent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SingleTaskActivity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;singleTaskIntent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_NEW_TASK&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_CLEAR_TASK&lt;/span&gt;
&lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;singleTaskIntent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// In SingleTaskActivity:&lt;/span&gt;
&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onNewIntent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onNewIntent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// Handle the new intent here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".SingleTaskActivity"&lt;/span&gt; &lt;span class="na"&gt;android:launchMode=&lt;/span&gt;&lt;span class="s"&gt;"singleTask"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Single Task Creates a new task and instantiates the activity at the root of the new task. If an instance already exists, it brings that task to the foreground.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Single Instance&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The behavior is the same as for "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task. Any activities started by this one open in a separate task.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Similar to singleTask, but even more restrictive. It allows only one instance of the activity to exist in the entire system, not just within a single task. Any other activity started from an activity with singleInstance mode will be launched in a new task. This mode is typically used for very specific scenarios, like splash screens or login activities that should only have one instance running at a time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example: Suppose, activity D has "launchMode = singleInstance". Launching D creates a new task with D at the root (D). Launching any other activity (like A) would create a new task with that activity at the root (A), leaving D's task unaffected (separate tasks: D and A).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Activity is launched in a new task, and this task can’t have any other activities.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Task1: &lt;code&gt;A -&amp;gt; B -&amp;gt; C&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Launching D results in:&lt;/li&gt;
&lt;li&gt;Task1: &lt;code&gt;A -&amp;gt; B -&amp;gt; C&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Task2: D (D is created in a separate task)&lt;/li&gt;
&lt;li&gt;Launching D again results in:&lt;/li&gt;
&lt;li&gt;Task1: &lt;code&gt;A -&amp;gt; B -&amp;gt; C&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Task2: &lt;code&gt;D&lt;/code&gt; (D is not created again, retrieved in its old state)&lt;/li&gt;
&lt;li&gt;Creating a new activity E (not on singleInstance mode) results in:&lt;/li&gt;
&lt;li&gt;Task1: &lt;code&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; E&lt;/code&gt; (E is created on task1)&lt;/li&gt;
&lt;li&gt;Task2: &lt;code&gt;D&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Single Instance launch mode example&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;singleInstanceIntent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SingleInstanceActivity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;singleInstanceIntent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_NEW_DOCUMENT&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_MULTIPLE_TASK&lt;/span&gt;
&lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;singleInstanceIntent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".SingleInstanceActivity"&lt;/span&gt; &lt;span class="na"&gt;android:launchMode=&lt;/span&gt;&lt;span class="s"&gt;"singleInstance"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;singleInstancePerTask&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The activity can only run as the root activity of the task, the first activity that created the task, and therefore there can only be one instance of this activity in a task. In contrast to the singleTask launch mode, this activity can be started in multiple instances in different tasks if the FLAG_ACTIVITY_MULTIPLE_TASK or FLAG_ACTIVITY_NEW_DOCUMENT flag is set.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: "singleTask" and "singleInstancePerTask" remove all activities that are above the starting activity from the task. For example, suppose a task consists of root activity A with activities B and C. The task is A-B-C, with C on top. An intent arrives for an activity of type A. If A's launch mode is "singleTask" or "singleInstancePerTask", the existing instance of A receives the intent through onNewIntent(). B and C are finished, and the task is now A.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Choosing the Right Launch Mode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The appropriate launch mode depends on the specific functionality you aim to achieve within your app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use standard mode for activities that don't require special handling of multiple instances or updates within a single instance.&lt;/li&gt;
&lt;li&gt;Use singleTop mode for activities that act as containers or entry points for tasks, where you only want one active instance at a time, and want to handle updates within that instance using onNewIntent().&lt;/li&gt;
&lt;li&gt;Use singleTask mode for activities that represent the core functionality of a distinct task, ensuring only one instance exists per task and potentially clearing the task stack to get there.&lt;/li&gt;
&lt;li&gt;Use singleInstance mode sparingly, for special cases where you truly need only one instance of an activity in the entire system (e.g., login or splash screen).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Android Activity Launch Modes Through Practical Implementation
&lt;/h2&gt;

&lt;p&gt;MainActivity Implementation: MainActivity acts as our control panel, with buttons to initiate activities with each launch mode. Here’s how to set it up:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// MainActivity.kt&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.content.Intent&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.os.Bundle&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.widget.Button&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.widget.Toast&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;androidx.appcompat.app.AppCompatActivity&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainActivity&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AppCompatActivity&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;setContentView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activity_main&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;buttonStandard&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findViewById&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button_standard_launch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;buttonStandard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setOnClickListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Standard Launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LENGTH_SHORT&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ActivityA&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;buttonSingleTop&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findViewById&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button_single_top_launch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;buttonSingleTop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setOnClickListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"SingleTop Launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LENGTH_SHORT&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ActivityA&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;buttonSingleTask&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findViewById&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button_single_task_launch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;buttonSingleTask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setOnClickListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"SingleTask Launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LENGTH_SHORT&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ActivityB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;buttonSingleInstance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findViewById&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button_single_instance_launch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;buttonSingleInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setOnClickListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"SingleInstance Launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LENGTH_SHORT&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ActivityC&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;buttonNewTaskClearTop&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findViewById&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button_new_task_clear_top_launch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;buttonNewTaskClearTop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setOnClickListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"New Task and Clear Top"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LENGTH_SHORT&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;intent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ActivityD&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_NEW_TASK&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_CLEAR_TOP&lt;/span&gt;
            &lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this code, we define buttons that, when clicked, start activities with different launch modes using intents. We also display toast messages to indicate which mode is being activated.&lt;/p&gt;

&lt;p&gt;XML Layout for Buttons: The user interface for MainActivity is defined in activity_main.xml. It includes buttons that correspond to each launch mode:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="c"&gt;&amp;lt;!-- activity_main.xml --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;LinearLayout&lt;/span&gt; &lt;span class="na"&gt;xmlns:android=&lt;/span&gt;&lt;span class="s"&gt;"http://schemas.android.com/apk/res/android"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
    &lt;span class="na"&gt;android:orientation=&lt;/span&gt;&lt;span class="s"&gt;"vertical"&lt;/span&gt;
    &lt;span class="na"&gt;android:padding=&lt;/span&gt;&lt;span class="s"&gt;"16dp"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- Standard Launch: Adds ActivityA on top of MainActivity --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/button_standard_launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"Standard Launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_marginBottom=&lt;/span&gt;&lt;span class="s"&gt;"8dp"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- SingleTop Launch: Adds ActivityA on top if not already there or brings it to top if it's already there --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/button_single_top_launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"SingleTop Launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_marginBottom=&lt;/span&gt;&lt;span class="s"&gt;"8dp"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- SingleTask Launch: Brings ActivityB to the top of the stack, clearing above it --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/button_single_task_launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"SingleTask Launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_marginBottom=&lt;/span&gt;&lt;span class="s"&gt;"8dp"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- SingleInstance Launch: Opens ActivityC in a new task with its own backstack --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/button_single_instance_launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"SingleInstance Launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_marginBottom=&lt;/span&gt;&lt;span class="s"&gt;"8dp"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- New Task and Clear Top: Starts ActivityD in a new task and clears any existing instances of it --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/button_new_task_clear_top_launch"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"New Task and Clear Top"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_marginBottom=&lt;/span&gt;&lt;span class="s"&gt;"8dp"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/LinearLayout&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Other layout files:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="c"&gt;&amp;lt;!-- activity_b.xml --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;RelativeLayout&lt;/span&gt; &lt;span class="na"&gt;xmlns:android=&lt;/span&gt;&lt;span class="s"&gt;"http://schemas.android.com/apk/res/android"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/button_start_activity_c"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"Start Activity C"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_centerInParent=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/RelativeLayout&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- activity_c.xml --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;RelativeLayout&lt;/span&gt; &lt;span class="na"&gt;xmlns:android=&lt;/span&gt;&lt;span class="s"&gt;"http://schemas.android.com/apk/res/android"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/button_start_activity_d"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"Start Activity D"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_centerInParent=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/RelativeLayout&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- activity_d.xml --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;RelativeLayout&lt;/span&gt; &lt;span class="na"&gt;xmlns:android=&lt;/span&gt;&lt;span class="s"&gt;"http://schemas.android.com/apk/res/android"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/button_start_activity_e"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"Start Activity E"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_centerInParent=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/RelativeLayout&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- activity_e.xml --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;RelativeLayout&lt;/span&gt; &lt;span class="na"&gt;xmlns:android=&lt;/span&gt;&lt;span class="s"&gt;"http://schemas.android.com/apk/res/android"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;
    &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"match_parent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- No button needed for Activity E as it's the last one in the sequence --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;TextView&lt;/span&gt;
        &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/text_view_activity_e"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_width=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_height=&lt;/span&gt;&lt;span class="s"&gt;"wrap_content"&lt;/span&gt;
        &lt;span class="na"&gt;android:text=&lt;/span&gt;&lt;span class="s"&gt;"Activity E"&lt;/span&gt;
        &lt;span class="na"&gt;android:layout_centerInParent=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/RelativeLayout&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Manifest for Example:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".MainActivity"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="c"&gt;&amp;lt;!-- MainActivity has standard launch mode by default --&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/activity&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".ActivityA"&lt;/span&gt;&lt;br&gt;
          &lt;span class="na"&gt;android:launchMode=&lt;/span&gt;&lt;span class="s"&gt;"singleTop"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="c"&gt;&amp;lt;!-- ActivityA has singleTop launch mode --&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/activity&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".ActivityB"&lt;/span&gt;&lt;br&gt;
          &lt;span class="na"&gt;android:launchMode=&lt;/span&gt;&lt;span class="s"&gt;"singleTask"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="c"&gt;&amp;lt;!-- ActivityB has singleTask launch mode --&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/activity&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".ActivityC"&lt;/span&gt;&lt;br&gt;
          &lt;span class="na"&gt;android:launchMode=&lt;/span&gt;&lt;span class="s"&gt;"singleInstance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="c"&gt;&amp;lt;!-- ActivityC has singleInstance launch mode --&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/activity&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".ActivityD"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="c"&gt;&amp;lt;!-- ActivityD will use flags for new task and clear top --&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/activity&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;activity&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".ActivityE"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="c"&gt;&amp;lt;!-- ActivityE has no specific launch mode requirements --&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/activity&amp;gt;&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tasks&lt;/strong&gt; are a collection of activities that users interact with when performing a certain job.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Back Stack&lt;/strong&gt; is the order in which activities are opened and stored in a task.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Launch Modes&lt;/strong&gt; determine how an activity instance is associated with a task and how it interacts with the back stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Launch modes allow developers to control the creation and organization of activities within tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Standard&lt;/strong&gt;: Creates a new instance every time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Top&lt;/strong&gt;: Reuses the top activity if it’s the same type being launched.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Task&lt;/strong&gt;: Ensures only one instance exists within a task, clearing others if necessary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Instance&lt;/strong&gt;: Ensures one instance across all tasks, ideal for activities that must not be interrupted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By choosing the appropriate launch mode, you can optimize the user experience by managing how activities are instantiated, reused, or cleared from memory. This knowledge helps in designing efficient, user-friendly app navigation flows.&lt;/p&gt;

</description>
      <category>android</category>
      <category>androiddev</category>
      <category>mobile</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Main components of android application</title>
      <dc:creator>Dilip Patel </dc:creator>
      <pubDate>Wed, 19 Jun 2024 08:01:06 +0000</pubDate>
      <link>https://dev.to/dilip2882/main-components-of-android-application-4n88</link>
      <guid>https://dev.to/dilip2882/main-components-of-android-application-4n88</guid>
      <description>&lt;h2&gt;
  
  
  Application Components
&lt;/h2&gt;

&lt;p&gt;Application components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your application’s overall behavior.&lt;/p&gt;




&lt;h2&gt;
  
  
  Activities
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;An activity represents a single, full-screen user interface (UI) within your application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each activity focuses on a specific task or feature, providing a cohesive and focused experience for the user.   It's the primary touchpoint for user interaction within your application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: &lt;code&gt;onCreate()&lt;/code&gt;, &lt;code&gt;onStart(&lt;/code&gt;), &lt;code&gt;onResume()&lt;/code&gt;, &lt;code&gt;onPause()&lt;/code&gt;, &lt;code&gt;onStop()&lt;/code&gt;, and &lt;code&gt;onDestroy()&lt;/code&gt;. The system invokes each of these callbacks as an activity enters a new state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's a basic example of an activity written in Kotlin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MainActivity : Activity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code defines a &lt;code&gt;MainActivity&lt;/code&gt;class that inherits from the Activity class. The &lt;code&gt;onCreate&lt;/code&gt;method, overridden here, is called when the activity is first created. This is where you would typically inflate your layout (the visual design of the screen) using &lt;code&gt;setContentView&lt;/code&gt;and set up UI elements for user interaction.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Activities manage the application's lifecycle, meaning they are created, paused, resumed, and destroyed as the user navigates through the app.&lt;/li&gt;
&lt;li&gt;Each activity is independent, allowing different applications to potentially launch specific activities within your app (if permissions allow).&lt;/li&gt;
&lt;li&gt;For a more feature-rich UI, activities often work together, passing data and triggering actions between them.&lt;/li&gt;
&lt;li&gt;Activities can handle user input through UI elements like buttons, text fields, and touch gestures.&lt;/li&gt;
&lt;li&gt;They can also launch other activities or system services within the Android framework.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Services
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A Service is an application component that can perform long-running operations in the background, and it doesn't provide a user interface.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unlike activities, &lt;strong&gt;services run in the background to perform long-running operations or tasks for remote processes.&lt;/strong&gt; They don't provide a UI directly. &lt;br&gt;
A service can be started, bound, or both:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Started&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A service is "started" when an application component (such as an activity) starts it by calling &lt;code&gt;startService()&lt;/code&gt;. Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself. The service runs indefinitely and must stop itself by calling &lt;code&gt;stopSelf()&lt;/code&gt;. Another component can also stop the service by calling &lt;code&gt;stopService()&lt;/code&gt;. When the service is stopped, the system destroys it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bound&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A service is "bound" when an application component binds to it by calling &lt;code&gt;bindService()&lt;/code&gt;. A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed. The service is created when another component (a client) calls &lt;code&gt;bindService()&lt;/code&gt;. The client then communicates with the service through an &lt;code&gt;IBinder&lt;/code&gt; interface. The client can close the connection by calling &lt;code&gt;unbindService()&lt;/code&gt;. Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. The service does not need to stop itself.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Foreground&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app. Although both Activities and Services can be killed if the system is low on memory, a foreground service has priority over other resources. To request that a service run in the foreground, call &lt;code&gt;startForeground()&lt;/code&gt; instead of &lt;code&gt;startService()&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's a simplified example of a service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyService : Service() {

  override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    // Perform background task here
    return START_STICKY
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code defines a &lt;code&gt;MyService&lt;/code&gt;class that extends the &lt;code&gt;Service&lt;/code&gt;class. The &lt;code&gt;onStartCommand&lt;/code&gt; method is called when the service is started, typically by an &lt;code&gt;activity&lt;/code&gt; or another component. This is where you would place your background operations, like playing music or fetching data from a network.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Key Points:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Services are ideal for long-running tasks that shouldn't be interrupted by the user interacting with the UI.&lt;/li&gt;
&lt;li&gt;They can be started, stopped, or bound to by other components in your application.&lt;/li&gt;
&lt;li&gt;There are different service lifecycle methods to handle various scenarios, such as when the system is running low on memory.&lt;/li&gt;
&lt;li&gt;Services can run even when the user is not actively using the application, as long as the system allows it.&lt;/li&gt;
&lt;li&gt;They can communicate with activities or other components through mechanisms like intents or binders.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Broadcast Receivers
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Broadcast receivers are lightweight components that listen for system-wide broadcast announcements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Broadcasts are messages that the Android system and Android apps send when events occur that might affect the functionality of other apps. These announcements can originate from the system itself (e.g., low battery) or from other applications. &lt;/p&gt;

&lt;p&gt;There are two types of broadcasts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;System broadcasts are delivered by the system.&lt;/li&gt;
&lt;li&gt;Custom broadcasts are delivered by your app.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Apps can receive broadcasts in two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Manifest-declared receivers&lt;/li&gt;
&lt;li&gt;Context-registered receivers&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;There are two kinds of Broadcast Receivers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed.&lt;/li&gt;
&lt;li&gt;Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's an example of a broadcast receiver:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class BatteryLevelReceiver : BroadcastReceiver() {

  override fun onReceive(context: Context, intent: Intent) {
    val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
    // Handle low battery level if needed
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Key Points:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Broadcast receivers are event-driven, meaning they are triggered only when a specific broadcast is sent. This makes them efficient for reacting to system changes or application events without constantly monitoring the system state.&lt;/li&gt;
&lt;li&gt;They are lightweight and have a simple lifecycle, making them suitable for short-lived tasks like updating the UI based on a broadcast.&lt;/li&gt;
&lt;li&gt;Broadcast receivers can be registered dynamically at runtime or declared statically in the Android Manifest file. This allows for flexibility in how you choose to listen for broadcasts.&lt;/li&gt;
&lt;li&gt;They cannot directly interact with the UI themselves. However, they can trigger actions within activities or services that can then update the UI or perform other necessary actions.&lt;/li&gt;
&lt;li&gt;Broadcast receivers can be permission-protected, ensuring that only authorized applications can receive specific broadcasts.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Content Providers
&lt;/h2&gt;

&lt;p&gt;Content providers act as a central hub for managing shared sets of application data. They allow other applications to access and potentially modify this data, if your content provider grants permission. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;These are handled by the class ContentResolver class. This class implements a set of APIs(Application Programming Interface) that enables the other applications to perform the transactions. Any Content Provider must implement the Parent Class of ContentProvider class. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's a peek at a content provider :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class NoteProvider : ContentProvider() {

  override fun onCreate(): Boolean {
    // Initialize content provider
    return true
  }

  // Implement methods for querying and modifying data
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code defines a &lt;code&gt;NoteProvider&lt;/code&gt; class extending the &lt;code&gt;ContentProvider&lt;/code&gt;class. The &lt;code&gt;onCreate&lt;/code&gt;method is used for initialization. Content providers offer a standard set of methods for other applications to interact with the data, including querying for specific information or updating existing data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Key Points:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Content providers are useful for sharing data between your application and others, enabling functionalities like contact access or data exchange.&lt;/li&gt;
&lt;li&gt;They can also be used for private data management within your application.&lt;/li&gt;
&lt;li&gt;Security is crucial when dealing with content providers, as you control access permissions for other applications.&lt;/li&gt;
&lt;li&gt;Content providers offer a structured way to access and modify data, often using a relational database model.&lt;/li&gt;
&lt;li&gt;They can be used to synchronize data between your application and other applications or services.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, Android applications are built upon four fundamental components: activities, services, content providers, and broadcast receivers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Activities&lt;/strong&gt; represent individual screens with user interfaces, providing the primary touchpoint for user interaction.&lt;br&gt;
&lt;strong&gt;Services&lt;/strong&gt; handle long-running background tasks, operating independently of the UI.&lt;br&gt;
&lt;strong&gt;Content providers&lt;/strong&gt; manage shared sets of data, enabling data exchange between your application and others.&lt;br&gt;
&lt;strong&gt;Broadcast receivers&lt;/strong&gt; act as event listeners, responding to system-wide announcements and triggering appropriate actions.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>androiddev</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
