<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by harish gyanani on Medium]]></title>
        <description><![CDATA[Stories by harish gyanani on Medium]]></description>
        <link>https://medium.com/@gyanani-harish?source=rss-d8963a44f926------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*FmRAO-5iwFzOI0E_.</url>
            <title>Stories by harish gyanani on Medium</title>
            <link>https://medium.com/@gyanani-harish?source=rss-d8963a44f926------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 19:56:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gyanani-harish/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Launch Modes in Android]]></title>
            <link>https://gyanani-harish.medium.com/launch-modes-in-android-53e82db0c863?source=rss-d8963a44f926------2</link>
            <guid isPermaLink="false">https://medium.com/p/53e82db0c863</guid>
            <dc:creator><![CDATA[harish gyanani]]></dc:creator>
            <pubDate>Fri, 05 Feb 2021 11:53:19 GMT</pubDate>
            <atom:updated>2021-02-06T16:32:07.952Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Contents…<br> ✔ What is a Launch Mode?<br> ✔ How a Stack/BackStack is arranged?<br> ✔ How to implement Launch Mode using a Manifest file?<br> ✔Types of Launch Mode…<br> 1) Standard<br> 2) SingleTop<br> 3) SingleTask<br> 4) SingleInstance <br> ✔ Using Intent Flags…</strong></h4><p>Launch modes allow you to define how a new instance of an activity is associated with the current task.</p><p>In simple words, Launch mode is an instruction for Android OS which specifies how the activity should be launched.</p><p><strong>Question. How Stack is arranged?</strong></p><p><strong>Answer.</strong> If we have more than one activities in our application, then the first one launched would be set at the bottom or at the root stage on the stack (stack can be referred to as a package which defines the arrangement and execution of activities) and the recent most activity launched would be at the top most level of stack.</p><p>Suppose we have executed Activity1, Activity2, Activity3, Activity4, Activity5 respectively (one after another).. then the stack will look like following…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*kJAaLOqOVmnaEVivejMpYQ.png" /></figure><p><strong>Using the manifest file</strong></p><p>When declaring an activity in your manifest file, you can specify how the activity should associate with a task using the <strong>&lt;activity&gt; </strong>element’s <strong>launch Mode </strong>attribute.</p><p>There are four launch modes for activity…</p><p>· standard</p><p>· single Top</p><p>· single Task</p><p>· single Instance</p><p><strong>1.) Standard:</strong></p><p>This is the default launch mode of activity. If you don’t set any launch mode to your activity, it will use the standard mode by default. It creates a new instance of activity every time even if activity instance is already present.</p><p>Suppose we have 1, 2, 3, 4, and 5 activities and your activity B has standard launch mode.</p><p>· Now again launching activity 3</p><p>· State of Activity Stack before launch 3</p><p>1 →2 → 3 →4</p><p>· State of Activity Stack after launch 3</p><p>1 → 2 → 3 →4 → 3</p><p>· We can see that new instance of 3 is created again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*99EmYlqU8_gc2j1_m1iXEQ.png" /></figure><p><strong>2.)Single Top:</strong></p><p>If an instance of activity already exists at the top of the current task, <strong>a new instance will not be created </strong>and the Android system will route the intent information through onNewIntent().</p><p>If an instance is not present on top of the task then a <strong>new instance will be created.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*wSzVfvZl0iJm6RFC7Tl2HA.png" /></figure><p>Suppose we have 1, 2, 3, and 4 activities. 1 →2 →3 →4</p><p>· If we launch 4 then a new instance of 4 will not be created as it is on the top of task/stack.</p><p>· So it will look like 1 →2 →3 →4</p><p>· If we launch 3 then a new instance of 3 will be created as it is not on top.</p><p>· Now suppose we have 1 →2 →3 →4 →3 like this , then we if again launch 3 activity then in this case new instance will not be created. Instead, we will receive the callback on onNewIntent() method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*99EmYlqU8_gc2j1_m1iXEQ.png" /></figure><p><strong>3.)Single Task:</strong></p><p>An activity declared with launch mode as single Task can have only one instance in the system (singleton). At a time only one instance of activity will exist.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*umTgKwWUXYtX1h7OCqQBBg.png" /></figure><p>Let’s suppose we have set activity 2 as single Task and then we will execute them in series (like 1 →2 → 3 →4 →5)</p><p>· If we call activity 2 after 5 then it will destroy 3 activities , i.e., act 5,4 and 3… and it will launch the original activity 2…</p><p>· State of Activity Stack before launch 2</p><p>1 →2 → 3 →4 →5 (call act 2)</p><p>· State of Activity Stack after launch 2</p><p>1 → 2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*oPFRTMpI6fonlhvbCODhjA.png" /></figure><p><strong>4.)Single Instance:</strong></p><p>Single Instance is quite like Single Task. The difference is, when first-time Single Instance activity is created, it will create a new Task and put activity at the root of this newly created task. Again, this is a specialized task which will have only one activity as its member.</p><p>In simple words , if we create an activity using single Instance mode , a new Task would be automatically created to place that new Activity.</p><p>If you again, call any other activity it will get out of this Task and create an instance in a previous task(if defined as standard or single Top mode) . And, every time you call this Single Instance activity it will route to the same activity of separate task by onNewIntent() method.</p><p>….</p><p>Suppose you have 1, 2, and 3 activities(1 →2 →3) and your activity 4 has a single Instance launch mode.</p><p>· In this case, if we launch 4 (1 →2 →3 →4) then 4 will be launch in the different task. <strong>New task for 4 will be created.</strong></p><p>· Task 1 : 1 →2 →3</p><p>· Task 2 : 4 (here 4 will be in the different task)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/209/1*hryoVKd38N0U8vs84RTZ6Q.png" /></figure><p>· Now if we continue this and start activity 5</p><p>(1 →2 →3 →4 →5) then Stack will look like…</p><p>· Task 1 : <strong>1 →2 →3 →5</strong></p><p>· Task 2 : <strong>4</strong></p><p><strong>Using Intent Flags:</strong></p><p>When starting an activity, you can modify the default association of an activity to its task by including flags in the intent that you deliver to <a href="https://developer.android.com/reference/android/app/Activity#startActivity(android.content.Intent)">startActivity()</a>. The flags you can use to modify the default behavior are:</p><p><a href="https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NEW_TASK">FLAG_ACTIVITY_NEW_TASK</a> : Start the activity in a new task or reuse an existing task tied to that activity. It behaves same as “singleTask” when used with FLAGS_ACTIVITY_CLEAR_TOP.</p><p>FLAG_ACTIVITY_CLEAR_TASK : This clears any existing task that would be associated with the Activity before the Activity is started. This Activity then becomes the new root of the task and old Activities are finished.</p><pre>FLAG_ACTIVITY_CLEAR_TASK is most often used with FLAG_ACTIVITY_NEW_TASK.</pre><pre>startActivity(intent.addFlags</pre><pre>(Intent.<em>FLAG_ACTIVITY_NEW_TASK </em>or Intent.<em>FLAG_ACTIVITY_CLEAR_TASK</em>))</pre><p>When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.</p><p>CLEAR_TASK is used to destroy other existing activities and NEW_TASK would make it a new task for the activity associated with these intent flags.</p><p>That is, the task id would remain same as before but only that activity would exist in the task on which these flags are implemented (in startActivity(…)).</p><p><a href="https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_SINGLE_TOP">FLAG_ACTIVITY_SINGLE_TOP</a><strong> : </strong>If the activity being started is the current activity (at the top of the back stack), then the existing instance receives a call to <a href="https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)">onNewIntent()</a>, instead of creating a new instance of the activity.</p><p>This produces the same behavior as the “singleTop” <a href="https://developer.android.com/guide/topics/manifest/activity-element#lmode">launchMode</a> value.</p><blockquote><strong>FLAGS_ACTIVITY_CLEAR_TOP</strong> alone used with the root activity destroys all the activity on it’s top when called again…<br><strong>FLAG_ACTIVITY_CLEAR_TASK</strong> when used with <strong>FLAG_ACTIVITY_NEW_TASK</strong>, it destroys all the existing activities in the stack including itself and then make that empty task a new task for the activity associated with both of these flags together. this activity would then be the only existing activity in that task. onNewIntent method is not called while using these flags.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53e82db0c863" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Alternative/Replacement of getExternalStorageDirectory for android Q+ versions.]]></title>
            <link>https://gyanani-harish.medium.com/alternative-replacement-of-getexternalstoragedirectory-for-android-q-versions-2e548791bfd5?source=rss-d8963a44f926------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e548791bfd5</guid>
            <dc:creator><![CDATA[harish gyanani]]></dc:creator>
            <pubDate>Fri, 05 Feb 2021 10:33:13 GMT</pubDate>
            <atom:updated>2021-02-06T16:01:19.695Z</atom:updated>
            <content:encoded><![CDATA[<h3>What was valid before Android Q for this purpose? …</h3><p>Earlier, we used to access phones external storage’s directories via using a public class Environment which provides us with some methods like <strong>getExternalStorageDirectory/ getExternalStoragePublicDirectories.</strong></p><p>We would just have to create a file and by calling <strong>Environment.getExternalStorageDirectory(</strong><em>Path, Filename</em><strong>),</strong> our file would get created on the specified path and then we can perform Read and Write operations on our file.</p><p>I had mentioned an example for creating a text file in phone’s <strong>document</strong> folder…</p><pre>class MyActivity : AppCompatActivity() {<br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        setContentView(R.layout.<em>activity_mine</em>)<br>        val textView: TextView = findViewById(R.id.<em>textView</em>)<br>        val myText: String = textView.<em>text</em>.toString()<br>        val file = File(Environment.getExternalStoragePublicDirectory(Environment.<em>DIRECTORY_DOCUMENTS</em>), <strong>&quot;abc.txt&quot;</strong>)<br><br>        val fileName = <strong>&quot;abc.txt&quot;<br>        </strong>val folder = Environment.getExternalStorageDirectory()<br>        val myFile = File(folder, fileName)<br>        val fStream = FileInputStream(myFile)<br>        val sBuffer = StringBuffer()<br>        var i: Int<br>        while (fStream.read().<em>also </em><strong>{ </strong>i = <strong>it } </strong>!= -1) {<br>            sBuffer.append(i.toChar())<br>        }<br>        fStream.close()<br>        val details = sBuffer.toString()<br>        textView.<em>text </em>= details<br>        writeTextData(myFile,details)<br><br>        if (!myFile.exists()) {<br>            try {<br>                myFile.createNewFile()<br>            } catch (e: Exception) {<br>                e.printStackTrace()<br>            }<br>        }<br>    }<br><br>    private fun writeTextData(file: File, data: String) {<br>        val fileOutputStream: FileOutputStream? = null<br>        try {<br>            fileOutputStream?.write(data.<em>toByteArray</em>())<br><br>            Toast.makeText(this, <strong>&quot;Done&quot; </strong>+ file.<em>absolutePath</em>, Toast.<em>LENGTH_SHORT</em>).show()<br>        } catch (e: java.lang.Exception) {<br>            e.printStackTrace()<br>        } finally {<br>            if (fileOutputStream != null) {<br>                try {<br>                    fileOutputStream.close()<br>                } catch (e: IOException) {<br>                    e.printStackTrace()<br>                }<br>            }<br>        }<br>    }<br>}</pre><h3>What &amp; Why has been changed?</h3><p>Now, from Android Q or Android 10 onwards, <strong>getExternalStorageDirectory </strong>method has been deprecated.<br> To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets <a href="https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q">Build.VERSION_CODES.Q</a>, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to <strong>alternatives</strong> such as <a href="https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)">Context#getExternalFilesDir(String)</a>, <a href="https://developer.android.com/reference/android/provider/MediaStore">MediaStore</a>, or <a href="https://developer.android.com/reference/android/content/Intent#ACTION_OPEN_DOCUMENT">Intent#ACTION_OPEN_DOCUMENT</a>.</p><h3>How to work now for same type of requirement?</h3><p>1. We can Use <strong>getExternalFilesDir</strong>(), <strong>getExternalCacheDir</strong>(), or <strong>getExternalMediaDirs</strong>() (methods on Context) instead of <strong>Environment.getExternalStorageDirectory</strong>() for fulfilling not same but little bit similar requirements. <br> From these methods, may be not get the desired alternative for our problem.</p><p>2. We can modify our app to be able to work with <strong>Uri, </strong>then…</p><p>a. Use <strong>MediaStore</strong>, <strong>ContentResolver</strong>, and <strong>insert</strong>() to get a Uri for a particular type of media (e.g., an image)</p><h3>MediaStore with ContentResolver</h3><p>Apps that have a focus on media — audio, video, and images — can use the MediaStore.<br> Note, though, that you need READ_EXTERNAL_STORAGE to be able to see other apps’ content in the MediaStore.</p><p>This method is going to allow us to store our file wherever we want to but for accessing those files that are created by apps other than ours, this method provide us a <strong>URI</strong>(Uniform Resource Identifier) of the file instead of exact file location. <br> URI comes in a form that is not easily decodable, for example “<strong>content://media/external/images/media/307751</strong>”.</p><p><strong>URI</strong> is provided by <strong>Content provider</strong> to the <strong>Content Resolver</strong> with a level of abstraction (Content Resolver doesn’t know from where Content Provider has brought that file from). Content Resolver then provides the URI to the user according to his request.</p><p>Below is the example of saving a <strong>text file</strong> in <strong>Documents</strong> folder of phone’s external storage using ContentResolver…</p><h3>SAVING TEXT FILE…</h3><pre><strong>class </strong>MyActivity : AppCompatActivity() {</pre><pre>    @RequiresApi(Build.VERSION_CODES.<em>Q</em>)</pre><pre>    <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {</pre><pre>        <strong>super</strong>.onCreate(savedInstanceState)</pre><pre>        setContentView(R.layout.<em>activity_mine</em>)</pre><pre>        <strong>val </strong>editText: EditText = findViewById(R.id.<em>edt</em>)</pre><pre>        <strong>val </strong>write: Button = findViewById(R.id.<em>Output</em>)</pre><pre>        <strong>val </strong>read: Button = findViewById(R.id.<em>Input</em>)</pre><pre>        <strong>val </strong>textView: TextView = findViewById(R.id.<em>textView</em>)</pre><pre>        <strong>val </strong>resolver = <strong>this</strong>.<em>contentResolver</em></pre><pre><em>        </em><strong>val </strong>contentValues = ContentValues().<em>apply </em><strong>{</strong></pre><pre><strong>            </strong>put(MediaStore.MediaColumns.<em>DISPLAY_NAME</em>, <strong>&quot;myDoc1&quot;</strong>)</pre><pre>            put(MediaStore.MediaColumns.<em>MIME_TYPE</em>, <strong>&quot;text/plain&quot;</strong>)</pre><pre>            put(MediaStore.MediaColumns.<em>RELATIVE_PATH</em>, <strong>&quot;Documents&quot;</strong>)</pre><pre>        <strong>}</strong></pre><pre><strong>        val </strong>uri: Uri? = resolver.insert(MediaStore.Files.getContentUri(<strong>&quot;external&quot;</strong>), contentValues)</pre><pre>        Log.d(<strong>&quot;Uri&quot;</strong>, <strong>&quot;</strong>$uri<strong>&quot;</strong>)</pre><pre>        write.setOnClickListener <strong>{</strong></pre><pre><strong>            val </strong>edt : String = editText.<em>text</em>.toString()</pre><pre>            <strong>if </strong>(uri != <strong>null</strong>) {</pre><pre>                resolver.openOutputStream(uri).<em>use </em><strong>{</strong></pre><pre><strong>                    it</strong>?.write(<strong>&quot;Harish Gyanani </strong>$edt<strong>&quot;</strong>.<em>toByteArray</em>())</pre><pre>                    <strong>it</strong>?.close()</pre><pre>                <strong>}</strong></pre><pre><strong>            </strong>}</pre><pre>        <strong>}</strong></pre><pre><strong>        </strong>read.setOnClickListener <strong>{</strong></pre><pre><strong>            if </strong>(uri != <strong>null</strong>) {</pre><pre>                resolver.openInputStream(uri).<em>use </em><strong>{</strong></pre><pre><strong>                    val </strong>data = ByteArray(<strong>50</strong>)</pre><pre>                    <strong>it</strong>?.read(data)</pre><pre>                    textView.<em>text </em>= <em>String</em>(data)</pre><pre>                <strong>}</strong></pre><pre><strong>            </strong>}</pre><pre>        <strong>}</strong></pre><pre><strong>    </strong>}</pre><pre>}</pre><h4>For saving an <strong>image</strong> in DCIM folder, we can modify above code as…</h4><h3>SAVING IMAGE…</h3><pre>val resolver = this.contentResolver</pre><pre>val contentValues = ContentValues().apply {</pre><pre>put(MediaStore.MediaColumns.DISPLAY_NAME, &quot;CuteKitten001&quot;)</pre><pre>put(MediaStore.MediaColumns.MIME_TYPE, &quot;image/jpeg&quot;)</pre><pre>put(MediaStore.MediaColumns.RELATIVE_PATH, &quot;DCIM/<em>filename</em>&quot;)</pre><pre>}</pre><pre>val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)</pre><pre>resolver.openOutputStream(uri).use {</pre><pre>// TODO something with the stream</pre><pre>}</pre><p><strong>· </strong>Above example will only work with the android version Q/10 and above because only these versions support RELATIVE_PATH.</p><p><strong>· ContentValues </strong>is a class that is used to store a set of values that the <a href="https://stuff.mit.edu/afs/sipb/project/android/docs/reference/android/content/ContentResolver.html"><strong>ContentResolver</strong></a> can process.</p><p><strong>· </strong>We don’t need to call <strong>ContentProvider</strong> as it is automatically called by <strong>ContentResolver </strong>as per activity’s requirement.</p><p><strong>· </strong>resolver.openOutputStream is used to perform write/modify/update function on the file located in external storage with the use of its uri <strong>(</strong>provided by ContentProvider<strong>).</strong></p><p><strong>· </strong>resolver.openInputStream is used to perform read function on the file.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e548791bfd5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>