<?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>Forem: Dilip Chandar</title>
    <description>The latest articles on Forem by Dilip Chandar (@dilip_chandar_58fce2b3b7b).</description>
    <link>https://forem.com/dilip_chandar_58fce2b3b7b</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%2F1746544%2F9215a989-b3a7-4f3d-b86b-29c6fec9c209.jpg</url>
      <title>Forem: Dilip Chandar</title>
      <link>https://forem.com/dilip_chandar_58fce2b3b7b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dilip_chandar_58fce2b3b7b"/>
    <language>en</language>
    <item>
      <title>Android Launch Modes</title>
      <dc:creator>Dilip Chandar</dc:creator>
      <pubDate>Tue, 09 Jul 2024 09:28:17 +0000</pubDate>
      <link>https://forem.com/dilip_chandar_58fce2b3b7b/android-launch-modes-2pjh</link>
      <guid>https://forem.com/dilip_chandar_58fce2b3b7b/android-launch-modes-2pjh</guid>
      <description>&lt;p&gt;Launch modes of android tell us how our activity instances will be maintained in activity history stack.&lt;/p&gt;

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

&lt;p&gt;There are 4 types of launch modes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;standard&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;singleTop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;singleTask&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;singleInstance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. standard&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the default launch mode of activity in android. We don’t have to explicitly specify this in AndroidManifest.xml. When an activity is of this launch mode type, it will have multiple instances.&lt;/p&gt;

&lt;p&gt;For example if Activity A, which is of standard launch mode type, whenever this activity is launched, it will create new instance of Activity A.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. singleTop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the next type of launch mode which can also have multiple instances of the activity similar to standard type but on certain conditions. We have to specify that for the required activity in AndroidManifest.xml like this&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;android:launchMode="singleTop"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Whenever an activity falls into this type, on launch of this particular activity, new instance of the activity will only be created if it is not on the top of the recent activity history stack. Otherwise existing instance which is present on top will be re-used.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Activity A(singleTop) -&amp;gt; Activity B(standard) -&amp;gt; Activity A(singleTop)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the above flow, Activity A is launched at first. Then Activity B is launched. Again Activity is launched. But as activity A is not in top and Activity B is in top, new instance of Activity A is created.&lt;/p&gt;

&lt;p&gt;In the same flow, if activity A is again launched, it will re-use existing instance since it was already present on top&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Activity A(singleTop) -&amp;gt; Activity B(standard) -&amp;gt; Activity A(singleTop)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. singleTask&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This type of launch mode can only have one instance of the activity and should be specified in AndroidManifest.xml like this&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;android:launchMode=”singleTask”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Whenever an activity falls into this type of launch mode, on launch of the particular activity, it will create new instance only once. And when launched again, it will re-use the existing instance regardless of whether it is in top or other order of history stack. And while re-using the instance, it will clear stack of all activities below it and then get launched.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Activity A (standard) -&amp;gt; Activity B (singleTop) -&amp;gt; Activity C (singleTask)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now if activity C is launched, it will clear history of activity A and activity B and Activity C will be the only activity left in history stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. singIeInstance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This type of launch mode can also have only one instance of the particular activity. We have to specify this launch mode in AndroidManifest.xml like this&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;android:launchMode=”singleInstance”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This type of launch mode is useful to separate the activity from existing stack and create the instance in a separate task. And in that task also, only one instance of the activity can exist.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Activity A (standard) -&amp;gt; Activity B (singleTop) -&amp;gt; Activity C (singleTask)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now if Activity D (singleInstance) is launched, the flow will be like below&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task or Stack 1:&lt;/strong&gt; &lt;em&gt;Activity A (standard) -&amp;gt; Activity B (singleTop) -&amp;gt; Activity C (singleTask)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task or Stack 2:&lt;/strong&gt; &lt;em&gt;Activity D (singleInstance)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As you can see, both are in separate history stack.&lt;/p&gt;

&lt;p&gt;Note: Whenever an existing instance has to be re-used in any one of the launch modes like singleTop, singleTask and singleInstance, a method called &lt;strong&gt;onNewIntent(Intent intent)&lt;/strong&gt; will be triggered.&lt;/p&gt;

&lt;p&gt;Happy coding..&lt;/p&gt;

&lt;p&gt;Let’s connect on LinkedIn &lt;a href="https://www.linkedin.com/in/dilip-chandar-97570158" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/dilip-chandar-97570158&lt;/a&gt;?&lt;/p&gt;

</description>
      <category>android</category>
      <category>androiddev</category>
    </item>
    <item>
      <title>Wireless Debugging in Android</title>
      <dc:creator>Dilip Chandar</dc:creator>
      <pubDate>Mon, 08 Jul 2024 10:22:12 +0000</pubDate>
      <link>https://forem.com/dilip_chandar_58fce2b3b7b/wireless-debugging-in-android-c3</link>
      <guid>https://forem.com/dilip_chandar_58fce2b3b7b/wireless-debugging-in-android-c3</guid>
      <description>&lt;p&gt;An interesting and useful feature in android is wireless debugging. This helps us achieve whatever we can do through USB cable like debugging, catching logs in logcat by unplugging our device once wireless connection is established.&lt;/p&gt;

&lt;p&gt;Before we establish a wireless connection, we have to keep our device connected through USB cable. Once developer options are enabled, we will be able to see our device in Android Studio. In this article, steps are covered for Mac OS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Open Terminal and go to platform-tools directory. For example&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd Library/Android/sdk/platform-tools&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Check if adb command is working by typing&lt;/p&gt;

&lt;p&gt;&lt;code&gt;adb devices&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; If Step 2 fails by a message zsh: command not found: adb, type the following in terminal. For example&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export ANDROID_HOME=/Users/dilipchandar/Library/Android/sdk&lt;br&gt;
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; After Step 3, entering the command adb devices will show us list of devices like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;List of devices attached&lt;br&gt;
28c95c50 device&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Now we need to establish TCP IP connection with a port number by typing following command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;adb tcpip 5555&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Above command will display -&amp;gt; restarting in TCP mode port: 5555&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Finally we can enter the following command after getting our IP Address from our phone’s WiFi Settings. For example if our IP Address is 192.168.1.4&lt;/p&gt;

&lt;p&gt;&lt;code&gt;adb connect 192.168.1.4:5555&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Above command will display -&amp;gt; connected to 192.168.1.4:5555&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will see the list of devices like the below&lt;/p&gt;

&lt;p&gt;&lt;code&gt;List of devices attached&lt;br&gt;
28c95c50 device&lt;br&gt;
192.168.1.4:5555 device (shows device is connected wirelessly)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After this, we can disconnect USB cable and check Android Studio. We will see the device with IP Address in logcat as shown in screenshot&lt;/p&gt;

&lt;p&gt;Please note that wireless connection may have to be reactivated if device goes offline by entering command from Step 6 again. That’s all about wireless debugging. Thanks for reading. Happy coding!!&lt;/p&gt;

&lt;p&gt;Let’s connect on LinkedIn &lt;a href="https://www.linkedin.com/in/dilip-chandar-97570158" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/dilip-chandar-97570158&lt;/a&gt;?&lt;/p&gt;

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