<?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 Abdurahmanli Samra on Medium]]></title>
        <description><![CDATA[Stories by Abdurahmanli Samra on Medium]]></description>
        <link>https://medium.com/@abdurahmanlisemra?source=rss-329691707394------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4VN0YpeAl1SAUeMgoH6Xcg.jpeg</url>
            <title>Stories by Abdurahmanli Samra on Medium</title>
            <link>https://medium.com/@abdurahmanlisemra?source=rss-329691707394------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 08:41:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdurahmanlisemra/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[Threads]]></title>
            <link>https://medium.com/@abdurahmanlisemra/threads-a7dd8d6854b4?source=rss-329691707394------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7dd8d6854b4</guid>
            <dc:creator><![CDATA[Abdurahmanli Samra]]></dc:creator>
            <pubDate>Thu, 17 Apr 2025 20:36:46 GMT</pubDate>
            <atom:updated>2025-04-17T20:36:46.465Z</atom:updated>
            <content:encoded><![CDATA[<p>Simply put, a thread is like a worker in your app. It’s a sequence of instructions that runs independently but shares the same memory space as other threads in your process.</p><p>Let’s imagine your app as a restaurant. The main thread is the waiter taking orders and serving customers — handling the UI. If that waiter also had to cook every dish (like fetching data or resizing images), customers would wait forever. That’s where additional threads come in — they’re the kitchen staff, working behind the scenes so the waiter can keep smiling.”</p><h4>Why Threads and Async Programming Matter in Mobile Development?</h4><p>Mobile apps live or die by their user experience. If the UI freezes even for a second, users notice it and if this continues, it can lead to a bad experience, which can even lead to the user uninstalling the app. For example, if we run a slow task, like downloading a file, on UI thread, the app locks up, and you might even get the dreaded ‘Application Not Responding’ (ANR) error.”</p><p><strong>Asynchronous</strong> — is a non-blocking architecture, which means it doesn’t block further execution while one or more operations are in progress.</p><p><strong>Synchronous</strong> — it follows a strict set of sequences, which means that operations are performed one at a time, in perfect order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uHQpov-AWQYmDOIp" /></figure><h3>Threads</h3><ol><li><strong>Main Thread (UI Thread) — </strong>The main thread is responsible for handling user interactions, updating the UI, and processing events.</li><li><strong>Worker Threads — </strong>Worker threads are secondary threads that perform tasks independently of the main thread. These threads can be used for various purposes, such as network requests, file I/O, or complex computations.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7dd8d6854b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interview Questions — Part 3]]></title>
            <link>https://medium.com/@abdurahmanlisemra/interview-questions-part-3-aac76db8654b?source=rss-329691707394------2</link>
            <guid isPermaLink="false">https://medium.com/p/aac76db8654b</guid>
            <dc:creator><![CDATA[Abdurahmanli Samra]]></dc:creator>
            <pubDate>Thu, 06 Mar 2025 13:58:06 GMT</pubDate>
            <atom:updated>2025-03-06T13:58:06.430Z</atom:updated>
            <content:encoded><![CDATA[<h3>Interview Questions — Part 3</h3><p>Conditions, Loops, Returns and jumps</p><h3>1.Conditions</h3><ol><li>If expression</li><li>When expression and Statement</li></ol><p>It is like <strong>switch</strong> in other programming lang. It runs when the condition or statement true, other blocks is not working. You can use when with or without a <strong>subject</strong>. when(x){..}</p><p>As an <strong>expression</strong>, when <strong>returns</strong> a <strong>value</strong> for later use in your code. If you use when as an expression, you have to <strong>cover all possible cases.</strong> The value of the first matching branch becomes the value of the overall expression. If you don&#39;t cover all cases, the compiler throws an error.</p><pre>val text = when (x) {<br>    1 -&gt; &quot;x == 1&quot;<br>    2 -&gt; &quot;x == 2&quot;<br>    else -&gt; &quot;x is neither 1 nor 2&quot;<br>}</pre><p>As a <strong>statement</strong>, when completes an action <strong>without</strong> <strong>returning</strong> anything of further use. If you use when as a statement, you<strong> don&#39;t</strong> have to <strong>cover</strong> <strong>all</strong> <strong>possible cases</strong>. In this example, some cases aren&#39;t covered, so nothing happens. However, no error occurs</p><pre>when (x) {<br>    1 -&gt; print(&quot;x == 1&quot;)<br>    2 -&gt; print(&quot;x == 2&quot;)<br>    else -&gt; print(&quot;x is neither 1 nor 2&quot;)<br>}</pre><p>If your when expression has a<strong> subject, </strong>you can use an <strong>else </strong>branch to make sure that all possible cases are covered, but it isn&#39;t mandatory. If your when expression <strong>doesn&#39;t</strong> <strong>have</strong> a <strong>subject</strong>, you <strong>must</strong> have an <strong>else </strong>branch or the compiler throws an error.</p><pre>val numericValue = when (getRandomBit()) {<br>    // No else branch is needed because all cases are covered , <br>    // but of all caese are not covered else brunch must be.<br>    Bit.ZERO -&gt; 0<br>    Bit.ONE -&gt; 1<br>}<br><br>when {<br>    a &gt; b -&gt; &quot;a is greater than b&quot;<br>    a &lt; b -&gt; &quot;a is less than b&quot;<br>    else -&gt; &quot;a is equal to b&quot;<br>}</pre><p>You can use when with<strong> is, !is</strong> for type check , combining <strong>multiple</strong> <strong>cases</strong>, check if value is <strong>in range </strong>or not (in 1..10 -&gt; print(“x is in the range”)),also check <strong>enum</strong> or <strong>sealed</strong> class match the type or not.</p><pre>when (animal) {<br>    // Checks if `animal` is `Dog`<br>    is Animal.Dog -&gt; feedDog()<br>}</pre><h3>2. Loops</h3><p><strong>For loop —</strong> The for loop <strong>iterates</strong> through anything that provides an <strong>iterator</strong>.</p><p>This means that it:</p><ul><li>has a member or an extension function iterator() that returns <strong>Iterator&lt;&gt;</strong>, which:</li><li>has a member or an extension function next()</li><li>has a member or an extension function hasNext() that returns Boolean.</li></ul><pre>for (i in 1..3) {<br>    print(i)<br>}<br><br>for (i in 6 downTo 0 step 2) {<br>    print(i)<br>}</pre><p>If you want to use it with array , you can use <strong>array.indices</strong></p><p><strong>While, Do-while —</strong> while and do-while loops process their body continuously while their condition is satisfied.</p><p>while checks the condition and, if it&#39;s satisfied, processes the body.</p><p>do-while processes the body and then checks the condition. If it&#39;s satisfied, the loop repeats.</p><h3><strong>3.Returns and jumps</strong></h3><p>Kotlin has <strong>three</strong> structural jump expressions:</p><ul><li>return by default returns from the nearest enclosing function or <a href="https://kotlinlang.org/docs/lambdas.html#anonymous-functions">anonymous function</a>.</li><li>break terminates the nearest enclosing loop.</li><li>continue proceeds to the next step of the nearest enclosing loop.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aac76db8654b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interview Question — Part 2]]></title>
            <link>https://medium.com/@abdurahmanlisemra/interview-question-part-2-19f21e83b27f?source=rss-329691707394------2</link>
            <guid isPermaLink="false">https://medium.com/p/19f21e83b27f</guid>
            <dc:creator><![CDATA[Abdurahmanli Samra]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 08:42:13 GMT</pubDate>
            <atom:updated>2025-03-06T14:00:15.424Z</atom:updated>
            <content:encoded><![CDATA[<h3>Interview Question — Part 2</h3><p>Type checks and casts</p><h3>1.Type checks and casts</h3><p>In Kotlin, you can perform type checks to check the type of an object at runtime. Type casts enable you to convert objects to a different type.</p><p><strong>is and !is operators — </strong>To perform a runtime check that identifies whether an object is to be a given type or else.(type check)</p><p><strong>Smart cast(Safe) — </strong>allow the compiler to automatically cast a variable to a specific type if it has already been checked. Note that smart casts work only when the compiler can guarantee that the variable won’t change between the check and its usage.Smart casts only work on immutable variables (val). If the variable is var, the compiler cannot guarantee that it won’t change between the check and usage, so smart casts won’t apply.</p><pre>fun demo(x: Any) {<br>    if (x is String) {<br>        println(x.length) // Automatically cast to String<br>    }<br>}</pre><p>Here, after checking x is String, Kotlin <strong>automatically</strong> treats x as a String, so we can call length without an explicit cast.</p><pre>var mutable: Any = &quot;Hello, Android&quot;<br>if (mutable is String) {<br>    // mutable.length // Error: Smart cast impossible<br>    mutable = 42 // Could change here, so no smart cast<br>    println(mutable.length) // nor work , Manual cast needed<br>}</pre><p>Smart casts work not only for if conditional expressions but also for <a href="https://kotlinlang.org/docs/control-flow.html#when-expressions-and-statements">when expressions</a> and <a href="https://kotlinlang.org/docs/control-flow.html#while-loops">while loops</a>:</p><pre>when (x) {<br>    is Int -&gt; print(x + 1)<br>}</pre><p><strong>Logical operators</strong> — The compiler can perform smart casts on the right-hand side of &amp;&amp; or || operators if there is a type check.</p><pre>if (x !is String || x.length == 0) return</pre><h4><strong>Unsafe casing</strong></h4><p>To explicitly cast an object to a non-nullable type, use the unsafe cast operator as. If the cast isn’t possible, the compiler <strong>throws</strong> an <strong>exception</strong>. This is why it’s called <strong>unsafe</strong>.</p><pre>val x: String = y as String</pre><h4><strong>“Safe” (nullable) cast operator</strong></h4><p>To avoid exceptions, use the safe cast operator as?, which returns null on failure.</p><pre>val x: String? = y as? String</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19f21e83b27f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interview Questions — Part 1]]></title>
            <link>https://medium.com/@abdurahmanlisemra/interview-questions-part-1-eb14289b1bd9?source=rss-329691707394------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb14289b1bd9</guid>
            <dc:creator><![CDATA[Abdurahmanli Samra]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 06:42:53 GMT</pubDate>
            <atom:updated>2025-03-06T13:59:38.252Z</atom:updated>
            <content:encoded><![CDATA[<p>== and === ,Lateinit, lazy, const val and val , Construtors, Kotlin vs Java, Types in Kotlin</p><h3><strong>1. Difference between == and === in Kotlin</strong></h3><p><strong>Structural Equality (==) → </strong>It checks for <strong>equals()</strong>. == checks if two things have the <strong>same conten</strong>t or <strong>value</strong>, regardless of whether they are the exact same object. It&#39;s like comparing if two apples look the same, even if they are not the same apple.</p><p><strong>Referential Equality (===)</strong> → It checks whether the two <strong>references</strong> point to the same object. === checks if two things are the exact same object in <strong>memory</strong>, not just if they have the same content. It&#39;s like checking if two variables are pointing to the exact same physical apple, not just apples that happen to look the same.Same variable , or same class it works same.Data class has no impact on <strong><em>Referential Equality (===). </em></strong>It works in the same way even if we have a simple class or data class.</p><p><strong>In Java</strong> , == check referential equality. equals() checks content.</p><h3>2. Lateinit, lazy, const val and val in Kotlin</h3><p><strong>Compile</strong> time — compiling your code , writing.</p><p><strong>Run</strong> time — a user is running your piece of software</p><p><strong>Lateinit</strong> → is like saying, &quot;I&#39;ll get ready before you need me”. Can <strong>only</strong> be used with var (mutable variables). The value is assigned <strong>at runtime</strong>, so if you forget to initialize it before use, the app will crash with an UninitializedPropertyAccessException. Cannot be used with <strong>primitive types</strong> like Int, Double, etc. Can not accept nullable.</p><p><strong>Lazy</strong> → is useful in a scenario when we want to create an object inside a class, but that <em>object creation is expensive </em>and that might <em>lead to a delay</em> in the creation of the object that is dependent on that expensive object. Used only with val (immutable variables). The variable is <strong>not created until it is used for the first time</strong> (lazy evaluation).</p><p><strong>Val</strong> → A <strong>runtime constant</strong> (assigned once but determined at runtime)(<strong>immutable</strong> variables). If you try to reassign a val variable, you will get a <strong>compile-time error</strong>, but its value is <strong>only known at runtime</strong>.</p><p><strong>Var → </strong>is <strong>runtime</strong> also. The value of a var variable <strong>can change</strong> during execution. Since the value is <strong>not fixed at compile time</strong>, the compiler <strong>does not</strong> know its final value beforehand.</p><p>var age = 22 // Compiler knows type but not final value<br>age = 23 // ✅ Allowed, happens at runtime</p><p><strong>Const Val →</strong> <strong>Compile-Time Constants , t</strong>he value is assigned <strong>before runtime</strong> and <strong>cannot be changed</strong>. Can <strong>only</strong> be used with val and <strong>must be a primitive type</strong> (String, Int, Double, etc). Can <strong>only</strong> be declared at <strong>top level, inside an object, or companion object</strong> (not inside regular classes or functions).</p><h3>3. Construtors</h3><p>Constructor is like a special function that gets called when you create an object (an instance) of a class.</p><p><strong>Primary constructo</strong>r — send properties in head section when the create instanse of the object.(argument)</p><pre>class Person(name: String, age: Int) {....} // Primary Constructor</pre><p><strong>Secondary Constructor </strong>— The secondary constructor in Kotlin is like a handy side entrance to your building. While the <strong>primary constructor </strong>sets up the <strong>main</strong> structure, the <strong>secondary</strong> constructor offers <strong>additional</strong> ways to customize or initialize your objects. It’s there for extra flexibility when creating instances of your class. Constructor key word is used.</p><pre>// Define a class with a primary constructor<br>class Person(val personName: String, val personAge: Int) {<br>    <br>    // Secondary constructor with a different parameter set<br>    constructor(name: String) : this(name, 0) {<br>        // Additional initialization code if needed<br>    }<br>}<br><br>// Create instances using both constructors<br>val john = Person(&quot;John Doe&quot;, 25)<br>val jane = Person(&quot;Jane Doe&quot;)<br><br>// Accessing properties of the created instances<br>println(&quot;John - Name: ${john.personName}, Age: ${john.personAge}&quot;)<br>println(&quot;Jane - Name: ${jane.personName}, Age: ${jane.personAge}&quot;)<br><br>// Output<br>John - Name: John Doe, Age: 25<br>Jane - Name: Jane Doe, Age: 0</pre><p><strong>How do you create an instance of a class using both the primary and secondary constructors?</strong> By using the secondary constructor and calling the primary constructor using the this keyword, ensuring consistent initialization.</p><p><strong>Init</strong> — It’s important to note that the <strong>init</strong> block is executed <strong>after</strong> the <strong>primary</strong> constructor is called but <strong>before</strong> the <strong>secondary</strong> constructor. A class can have<strong> more than one</strong> init block, in this case, the initializer blocks are executed in the same order as they appear in the class body considering the properties if there are any in between.</p><h3>4.Kotlin vs Java.</h3><p>Both Kotlin and Java are platform independent. It means both .java or .kt file is converted to the .class file via java or kotlin compiler and then JVM will execute it. Why Koltin for android development?</p><p>Kotlin is <strong>simpler</strong>, understandable than Java. It reducing boilerplate code.</p><p>Kotlin has <strong>null</strong>-<strong>safety</strong>, preventing Android’s common NullPointerExceptions.</p><p><strong>Type inference —</strong> Kotlin supports type inference and automatically identifies the data type of a declared variable.</p><p><strong>Data</strong> <strong>classes</strong> — java does not have data class keyword. You can do same feature by using extra coding</p><p><strong>Extension</strong> <strong>Function</strong> — are available in Kotlin , but java it is not. You can do same feature by using extra coding.</p><p><strong>Async</strong> <strong>programming</strong> — In Jave threades are used but it is complex. In kotlin coroutines are used and it is simple.</p><p><strong>Jetpack</strong> <strong>Compose</strong> — just kotlin can be used.</p><h3>5.Types in Kotlin</h3><h4><strong>Numbers</strong> —Byte , Short , Int , Long. Float, Double</h4><p>When you initialize a variable with no explicit type specification, the compiler automatically infers the type with the <strong>smallest</strong> range enough to represent the value starting from Int.The JVM stores numbers as primitive types: int, double, and so on. When you use <a href="https://kotlinlang.org/docs/generics.html"><strong>generic types</strong></a><strong> </strong>or create a <strong>nullable</strong> number reference such as Int?, numbers are <strong>boxed</strong> in Java classes such as Integer or Double.</p><pre>var a = 54 -&gt; int in Java , primitive<br>var b : Int = 56 -&gt; int in java , primitive<br>var c : Int? = 67 -&gt; Integer object , non primitive</pre><p>The JVM applies a <a href="https://docs.oracle.com/javase/specs/jls/se22/html/jls-5.html#jls-5.1.7">memory optimization technique</a> to Integer and other objects that represent numbers between −128 and 127. All nullable references to such objects refer to the same cached object. For example, nullable objects in the following code are <a href="https://kotlinlang.org/docs/equality.html#referential-equality">referentially equal</a>:</p><pre>val a: Int = 100 // for 1000<br>val boxedA: Int? = a<br>val anotherBoxedA: Int? = a<br>println(boxedA === anotherBoxedA) // true, for 1000 it will be false<br>println(boxedB == anotherBoxedB) // true, for 1000 true</pre><p><strong>Type Conversion</strong> — toInt() , toDouble() ….. implicit type conversion in Kotlin is not allowed, in Java it works. For that reason for type conversion you have to use explicit methods.</p><h4><strong>String</strong></h4><p>Strings are immutable. Once you initialize a string, you can’t change its value or assign a new value to it. All operations that transform strings return their results in a new String object, leaving the original string unchanged.</p><pre>// Creates and prints a new String object<br>println(str.uppercase())<br>// ABCD<br>// The original string remains the same<br>println(str) <br>// abcd<br>Template expressions - using $ sign.</pre><p><strong>Template expressions</strong> — using <strong>$</strong> sign.</p><h4>Array</h4><p>An array is a data structure that holds a fixed number of values of the same type or its subtypes. Arrays are fixed in size. The only way to add or remove elements from an array is to create a new array each time, which is very inefficient.</p><p>When Should You Use Arrays?</p><ul><li><strong>Performance-critical applications</strong><br>Arrays are <strong>faster than collections</strong> because they have <strong>less overhead</strong> and are stored in a <strong>contiguous memory block</strong>, making them efficient for <strong>random access</strong> and <strong>low-level optimizations</strong>.</li><li><strong>Building custom data structures</strong><br>If you are implementing things like a <strong>custom queue, stack, or tree</strong>, arrays may be the right choice because they provide more control over memory management.</li></ul><p>To create arrays in Kotlin, you can use: functions, such as <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/array-of.html">arrayOf()</a>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/array-of-nulls.html#kotlin$arrayOfNulls(kotlin.Int)">arrayOfNulls()</a> or <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/empty-array.html">emptyArray()</a> , the Array constructor.</p><p>Convert arrays to collections — toList() , toSet() , toMap()(Only an array of <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-pair/">Pair&lt;K,V&gt;</a> can be converted to a Map)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb14289b1bd9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection. Koin]]></title>
            <link>https://medium.com/@abdurahmanlisemra/dependency-injection-koin-09164ea18c00?source=rss-329691707394------2</link>
            <guid isPermaLink="false">https://medium.com/p/09164ea18c00</guid>
            <category><![CDATA[dependency-injection]]></category>
            <dc:creator><![CDATA[Abdurahmanli Samra]]></dc:creator>
            <pubDate>Mon, 18 Nov 2024 12:56:25 GMT</pubDate>
            <atom:updated>2024-11-18T12:56:25.687Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Dependency injection</strong> is a software design pattern used to implement <strong>inversion of control (IoC)</strong> in which the dependencies of a class are provided from the outside rather than created within the class itself. This approach helps in decoupling classes, making them more modular, testable, and maintainable.</p><p>Without dependency injection , representing a Car that creates its own Engine dependency in code looks like this:</p><pre>class Car {<br><br>    private val engine = Engine()<br><br>    fun start() {<br>        engine.start()<br>    }<br>}<br><br>fun main(args: Array) {<br>    val car = Car()<br>    car.start()<br>}</pre><p>Car and Engine are tightly coupled - an instance of Car uses one type of Engine, and no subclasses or alternative implementations can easily be used.</p><pre>class Car(private val engine: Engine) {<br>    fun start() {<br>        engine.start()<br>    }<br>}<br><br>fun main(args: Array) {<br>    val engine = Engine()<br>    val car = Car(engine)<br>    car.start()<br>}</pre><pre>open class Engine {<br>    open fun start() {<br>        println(&quot;start from engine&quot;)<br>    }<br>}<br><br>class GasEngine : Engine() {<br>    override fun start() {<br>        println(&quot;start from gas&quot;)<br><br>    }<br>}<br><br>class Car(val engine: Engine) {<br>    fun startCar() {<br>        engine.start()<br>    }<br>}<br><br>fun main() {<br>    val car = Car(Engine())<br>    car.startCar()<br><br>    val gasEngine = GasEngine()<br>    val car2 = Car(gasEngine)<br>    car2.startCar()<br>}</pre><p>There are two major ways to do dependency injection in Android:</p><ul><li><strong>Constructor Injection</strong>. This is the way described above. You pass the dependencies of a class to its constructor.</li><li><strong>Field Injection (or Setter Injection)</strong>. Certain Android framework classes such as activities and fragments are instantiated by the system, so constructor injection is not possible. With field injection, dependencies are instantiated after the class is created. The code would look like this:</li></ul><pre>class Car {<br>    lateinit var engine: Engine<br><br>    fun start() {<br>        engine.start()<br>    }<br>}<br><br>fun main(args: Array) {<br>    val car = Car()<br>    car.engine = Engine()<br>    car.start()<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=09164ea18c00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SharedPreferences and EncryptedSharedPrefence]]></title>
            <link>https://medium.com/@abdurahmanlisemra/encryptedsharedprefence-07c6a3d20e29?source=rss-329691707394------2</link>
            <guid isPermaLink="false">https://medium.com/p/07c6a3d20e29</guid>
            <dc:creator><![CDATA[Abdurahmanli Samra]]></dc:creator>
            <pubDate>Mon, 18 Nov 2024 09:08:55 GMT</pubDate>
            <atom:updated>2024-11-18T09:09:23.571Z</atom:updated>
            <content:encoded><![CDATA[<p>Android Jetpack introduced a new library <a href="https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences">EncryptedSharedPrefences</a> that works on top of <strong>SharedPreferences</strong> but actually <strong>encrypts</strong> all that is stored. So if someone accesses the saved XML file, that would be encrypted and not readable.</p><p>The encryption and decryption keys are stored in the<em> </em><strong><em>hardware-backed keystore,</em></strong> providing an extra layer of security. Master key is stored in keystore. The Keystore is a specialized, isolated component of the Android OS that securely stores cryptographic keys.</p><pre>private fun createMasterKey(context: Context) =<br>    MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)<br>        .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)<br>        .build()</pre><pre>  sharedPref =<br>            EncryptedSharedPreferences.create(<br>                context, &quot;secret_shared_prefs&quot;,<br>                createMasterKey(context),<br>                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,<br>                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM<br>            )</pre><p>Here Ae256 is encryption method.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=07c6a3d20e29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RecyclerView vs ListView. Understanding ListAdapter and DiffUtil]]></title>
            <link>https://medium.com/@abdurahmanlisemra/recyclerview-vs-listview-understanding-listadapter-and-diffutil-c78d71d10c67?source=rss-329691707394------2</link>
            <guid isPermaLink="false">https://medium.com/p/c78d71d10c67</guid>
            <category><![CDATA[listadapter]]></category>
            <category><![CDATA[recyclerview-in-android]]></category>
            <category><![CDATA[diff-util]]></category>
            <dc:creator><![CDATA[Abdurahmanli Samra]]></dc:creator>
            <pubDate>Sat, 16 Nov 2024 13:59:44 GMT</pubDate>
            <atom:updated>2024-11-16T13:59:44.024Z</atom:updated>
            <content:encoded><![CDATA[<h3>Recycler view and List view</h3><p>ListView and RecyclerView are both popular ways to display a list of data items efficiently in Android. However, they handle data differently, which affects their performance and memory usage. Let’s explain how they work.</p><h4>How They Work</h4><p>Imagine you have a dataset with <strong>1,000 elements</strong> and want to display these elements on your screen. Let’s say only <strong>10 elements</strong> are visible at once on your phone. <em>How ListView and RecyclerView handle this situation?</em></p><p><strong><em>ListView:</em></strong></p><ul><li><strong>ListView</strong> tries to <strong>load all 1,000 elements</strong> at once before displaying them on the screen.</li><li>This approach is <strong>not efficient</strong> because it consumes a lot of memory, which can lead to <strong>memory leaks</strong> or app crashes, especially with large datasets.</li><li>Users may also experience <strong>delays</strong> or <strong>lag</strong> since the app must load all elements initially, even if most of them are not visible.</li></ul><p><strong><em>RecyclerView</em></strong>:</p><ul><li><strong>RecyclerView</strong> only loads the <strong>visible items</strong> (e.g., 10 elements) and a few additional items (usually <strong>2–4 extra items</strong>) above and below the visible part.</li><li>As the user scrolls, RecyclerView <strong>recycles</strong> these views, <strong>reusing</strong> them to display new items that come into view, instead of creating new views each time.</li><li>This method prevents delays and ensures <strong>smooth scrolling</strong> because it only loads the necessary views and updates them dynamically.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*35RxKr6EVrxRY2ZI.png" /></figure><p>RecyclerView also has a <strong><em>Layout Manage</em></strong><em>r</em>. In other words, using this feature allows data/items to be listed on the screen as desired.</p><p>There are<strong> <em>3 types</em></strong> of Layout Managers</p><ol><li>Linear Layout Manager — vertical or horizontal</li><li>Grid Layout Manager</li><li>Staggered Grid Layout Manager</li></ol><h3>List Adapter</h3><p>For displaying a list of <em>static dat</em>a, the default adapter works well. However, in most real-world scenarios, the data in a RecyclerView is <em>dynamic</em>. You may need to add, remove, update, or move items in your list. Traditionally, you would call notifyDataSetChanged<strong>()</strong> to refresh the data, but this method <strong>redraws the entire list</strong>, including unchanged parts, making it an expensive operation.</p><p>In contrast, ListAdapter efficiently handles these changes <strong>without</strong> <strong>redrawing</strong> the entire view. It calculates the differences between the old and new data using DiffUtil, and only updates the modified items. This approach not only optimizes performance but also provides smooth animations for item changes.</p><h3>DiffUtil</h3><p>DiffUtil is the key component that enables ListAdapter to update list items efficiently. DiffUtil compares and change the <em>new list</em> with the <em>old list</em> to figure out what was added, moved, updated and removed.</p><p>In order to identify new data, DiffUtil requires you to override areItemsTheSame() and areContentsTheSame(). areItemsTheSame() checks if two items are actually the same item. areContentsTheSame() checks if two items have the same data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9e1t2ZJfpOB_XtEz" /><figcaption>Diagram showing how areItemsTheSame() compares items.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*70oGB6weCYPfEQ6P.png" /><figcaption>Diagram showing how areContentsTheSame() compares items.</figcaption></figure><p>DiffUtil detect changes using <strong><em>Myers’ difference algorithm</em></strong>. We will use<strong> AsyncListDiffer, </strong>a <strong>DiffUtil</strong> helper that calculates the difference between two lists on a <strong>background</strong> <strong>thread</strong>. After calculating the difference, it signals the adapter of the changes in the two lists. For notify adapter these methods are used: notifyItemInserted(), notifyItemRemoved(), or notifyItemChanged(), ensuring that<em> </em><strong><em>only the modified items are updated in the UI</em></strong>, which improves performance and avoids unnecessary redraws.</p><h3>Conclusion</h3><p>Using <strong>ListAdapter</strong> simplifies handling dynamic data changes efficiently in RecyclerView. It relies on <strong>DiffUtil</strong> to detect differences between the current dataset and the new one. <strong>DiffUtil</strong> performs this comparison on a <strong>background thread</strong>, ensuring that the <strong>UI thread remains unblocked</strong>. To update the list in the ListAdapter, you call the <strong>submitList()</strong> method. This method sends the new list to the adapter, initiates the DiffUtil calculation, and applies the changes. As a result, only the necessary UI updates are performed, making the adapter highly efficient and smooth in handling dynamic data changes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c78d71d10c67" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>