<?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: MittapalliHareesh</title>
    <description>The latest articles on DEV Community by MittapalliHareesh (@mittapallihareesh).</description>
    <link>https://dev.to/mittapallihareesh</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%2F429232%2Fb1645e3e-d713-4963-af89-af6afe53e01b.png</url>
      <title>DEV Community: MittapalliHareesh</title>
      <link>https://dev.to/mittapallihareesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mittapallihareesh"/>
    <language>en</language>
    <item>
      <title>LaunchModes in Android</title>
      <dc:creator>MittapalliHareesh</dc:creator>
      <pubDate>Sat, 11 Jul 2020 13:37:59 +0000</pubDate>
      <link>https://dev.to/mittapallihareesh/launchmodes-in-android-46o1</link>
      <guid>https://dev.to/mittapallihareesh/launchmodes-in-android-46o1</guid>
      <description>&lt;p&gt;LaunchModes is specific to the android concept. It's one of the interview questions. We will see below what are all the launch modes have in android.&lt;/p&gt;

&lt;p&gt;There are 4 types of launch modes&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;If we wanted to use any of these, we have to explicitly declare in the AndroidManifest file by using the keyword as "android:launchMode"&lt;/p&gt;

&lt;p&gt;Ex:         android:launchMode="singleTask"&lt;/p&gt;

&lt;p&gt;Instead of declaring in the manifest file, we can even set in Java/Kotlin code by using Intent flags. &lt;/p&gt;

&lt;p&gt;FLAG_ACTIVITY_SINGLE_TOP -&amp;gt; Which is similar to singleTop&lt;br&gt;
FLAG_ACTIVITY_NEW_TASK -&amp;gt; Which is similar to singleTask&lt;br&gt;
FLAG_ACTIVITY_CLEAR_TOP &lt;/p&gt;

&lt;p&gt;Will see more about each launch mode's below individually.&lt;/p&gt;

&lt;p&gt;-----------------**************************---------------&lt;/p&gt;

&lt;h1&gt;
  
  
  Standard:-
&lt;/h1&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; This is a default launch mode in android. If you won't specify anything in the manifest file then the launch mode type is "standard". Android internally maintains stack. Whatever we open activity by default it will be added to the stack.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For example, we have four different types of activities like A, B, C, and D.&lt;/p&gt;

&lt;p&gt;Now from Activity A -&amp;gt; launch Activity B -&amp;gt; launched Activity C -&amp;gt; launched Activity D. (A -&amp;gt; B -&amp;gt; C -&amp;gt; D)&lt;/p&gt;

&lt;p&gt;All these activities will open in a single stack. Now from Activity_D try to open Activity_B, then a new instance of Activity_B will be created and it will be added to the same back stack. Now will be like&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         (A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; B)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h1&gt;
  
  
  SingleTop:-
&lt;/h1&gt;

&lt;p&gt;If we want to use this launch mode then in manifest.xml for the respective activity we have to set like  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   android:launchMode="singleTop"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For example, we have four different types of activities like Activity_A, Activity_B, Activity_C, and Activity_D.&lt;/p&gt;

&lt;p&gt;If we launch activities like A-&amp;gt;B-&amp;gt;C-&amp;gt;D from D if we try to launch C it will create a new instance and it will be added to back stack and again if try to launch C it will create one more new instance and it will be added to back stack.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; C -&amp;gt; C
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We declared Activity_C as "singleTop" in manifest like below.&lt;/p&gt;

&lt;p&gt;
      android:name= Activity_C&lt;br&gt;
      android:launchMode="singleTop"/&amp;gt;&lt;/p&gt;

&lt;p&gt;Now, if we launch activities like A-&amp;gt;B-&amp;gt;C-&amp;gt;D from D if we try to launch C it will create a new instance and it will be added to back stack and again if try to launch "Activity_C" it will not because already "Activity_C" is on top. In this case "&lt;em&gt;onNewIntent()&lt;/em&gt;" method will be called.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; C  (==&amp;gt; here again try to launch C it won't because "C" is a singleTop)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;below case's are possible&lt;br&gt;
          A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; C -&amp;gt; B -&amp;gt; C&lt;br&gt;
          A -&amp;gt; B -&amp;gt; D -&amp;gt; C -&amp;gt; B -&amp;gt; B -&amp;gt; C -&amp;gt; A -&amp;gt; C  &lt;/p&gt;

&lt;h1&gt;
  
  
  SingleTask:-
&lt;/h1&gt;

&lt;p&gt;SingleTask is most frequently used by developers. The main advantage of singleTask is only one instance of activity will be present in a stack.&lt;/p&gt;

&lt;p&gt;If we launch activities like A-&amp;gt;B-&amp;gt;C-&amp;gt;D from D if we try to launch C it will create a new instance and it will be added to back stack and again if try to launch C it will create one more new instance and it will be added to back stack.&lt;/p&gt;

&lt;p&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; C -&amp;gt; C&lt;/p&gt;

&lt;p&gt;Now, "Activity_C" defined as &lt;em&gt;singleTask&lt;/em&gt; we have declared like &lt;/p&gt;

&lt;p&gt;
      android:name= Activity_C&lt;br&gt;
      android:launchMode="singleTask"/&amp;gt;&lt;/p&gt;

&lt;p&gt;then, if we launch activities like A-&amp;gt;B-&amp;gt;C-&amp;gt;D from D if we try to launch "Activity_C" then what are all activities opened on top of Activity_C will be cleared. Now stack will have only&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       A -&amp;gt; B -&amp;gt; C 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;here "Activity_C" is a singleTask so if we try to launch "Activity_C" again "&lt;em&gt;onNewIntent()&lt;/em&gt;" method will be called it won't create a new instance and what are all activities opened on top of "Activity_C" will be poped off.&lt;/p&gt;

&lt;h1&gt;
  
  
  SingleInstance:-
&lt;/h1&gt;

&lt;p&gt;The name itself says stack will be only one instance. Will see the example below.&lt;/p&gt;

&lt;p&gt;If we launch activities like A-&amp;gt;B-&amp;gt;C-&amp;gt;D from D if we try to launch C it will create a new instance and it will be added to back stack and again if try to launch C it will create one more new instance and it will be added to back stack.&lt;/p&gt;

&lt;p&gt;A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; C -&amp;gt; C&lt;/p&gt;

&lt;p&gt;Now, "Activity_C" defined as &lt;em&gt;singleInstance&lt;/em&gt; we have declared like &lt;/p&gt;

&lt;p&gt;
      android:name= Activity_C&lt;br&gt;
      android:launchMode="singleInstance"/&amp;gt;&lt;/p&gt;

&lt;p&gt;then, if we launch activities like A-&amp;gt;B and from "B" try to launch "Activity_C" then it will be opened in new stack, it will not open in the same stack.&lt;/p&gt;

&lt;p&gt;like     A -&amp;gt; B  is in one stack.&lt;br&gt;
only     C will be in another stack.&lt;/p&gt;

&lt;p&gt;As of now, we launched A, B, and C activities, now if we try to launch "Activity_D" then which this activity will be added to which stack?&lt;/p&gt;

&lt;p&gt;The answer is it will be added to the first stack only means &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       A -&amp;gt; B -&amp;gt; D  is in one stack.
       C will be in another stack.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;why Activity_D pushed into the first stack? why not newly created stack. Here we have to understand the concept called "taskAffinefty" &lt;/p&gt;

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