<?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 Marc Garcia on Medium]]></title>
        <description><![CDATA[Stories by Marc Garcia on Medium]]></description>
        <link>https://medium.com/@marc.garcia?source=rss-116f8e2f3e10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LtLFF7EGHpNRWdRmjGinSw.jpeg</url>
            <title>Stories by Marc Garcia on Medium</title>
            <link>https://medium.com/@marc.garcia?source=rss-116f8e2f3e10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 04:49:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marc.garcia/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[Summary of #31DaysOfKotlin]]></title>
            <link>https://proandroiddev.com/summary-of-31daysofkotlin-68319d1d4972?source=rss-116f8e2f3e10------2</link>
            <guid isPermaLink="false">https://medium.com/p/68319d1d4972</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Marc Garcia]]></dc:creator>
            <pubDate>Sat, 31 Mar 2018 16:24:50 GMT</pubDate>
            <atom:updated>2018-04-02T08:09:54.617Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TMBXTv24qILzN3yckj3C7w.png" /></figure><p>During the month of March, <a href="http://twitter.com/AndroidDev">@AndroidDev</a> had a nice initiative tweeting, every day, a piece of code with some of the more powerful and useful features that Kotlin offers. I have collected all of them in this story 😊</p><h4><strong>1. Elvis Operator</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*S5Ge9MaRWlaDav5q4TDrCA.jpeg" /></figure><p>Check out the elvis operator ?:, to cut your “null-erplate”. It’s just a small bit of syntax sugar to replace nulls with a default value or even return! Check the <a href="https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/969249389582548997"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/661284d96d7cab3d83dafa50df9db56c/href">https://medium.com/media/661284d96d7cab3d83dafa50df9db56c/href</a></iframe><h4>2. String Templates</h4><p>Refer to variables and expressions in string literals by putting `$` in front of the variable name. Evaluate expressions using `${expression}`. Check the <a href="https://kotlinlang.org/docs/reference/basic-types.html#string-templates"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/969603241968357378"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/193e98d869231ea2c86a57f64cf990f0/href">https://medium.com/media/193e98d869231ea2c86a57f64cf990f0/href</a></iframe><h4>3. Destructuring Declarations</h4><p><a href="https://github.com/android/android-ktx">Android KTX</a> uses destructuring to assign the component values of a color. You can use destructuring in your classes, or extend existing classes to add destructuring. Check the <a href="https://kotlinlang.org/docs/reference/multi-declarations.html"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/969965627929694209"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20c0294dee036f1ca7f4d7879b425e00/href">https://medium.com/media/20c0294dee036f1ca7f4d7879b425e00/href</a></iframe><h4>4. When Expression</h4><p>Kotlin’s when expression can match on just about anything. Literal values, enums, ranges of numbers. You can even call arbitrary functions! Check the <a href="https://kotlinlang.org/docs/reference/control-flow.html#when-expression"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/970328015505850370"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ae45e7004cb6861ca6191f3a562fb4a/href">https://medium.com/media/2ae45e7004cb6861ca6191f3a562fb4a/href</a></iframe><h4>5. For loops</h4><p>For loops get superpowers when used with two other Kotlin features: range expressions and destructuring. Check <a href="https://kotlinlang.org/docs/reference/ranges.html"><strong>Ranges</strong></a><strong>, </strong><a href="https://kotlinlang.org/docs/reference/multi-declarations.html#destructuring-declarations"><strong>Destructuring</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/970690401567993861"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d558b56d91cacce89f115518240a922b/href">https://medium.com/media/d558b56d91cacce89f115518240a922b/href</a></iframe><h4>6. Properties and Fields</h4><p>In Kotlin, classes can have mutable and read-only properties, with getters and setters generated by default. You can also implement custom ones if required. Check the <a href="https://kotlinlang.org/docs/reference/properties.html"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/971052790155022336"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39df6f19b5929d500c69d05ecc579cc6/href">https://medium.com/media/39df6f19b5929d500c69d05ecc579cc6/href</a></iframe><h4>7. Data Classes</h4><p>Creating classes with one role: to hold data? Mark them as “data” classes. The default implementation of equals() is generated (so are hashCode(), toString(), and copy()) and checks for structural equality. Check the <a href="https://kotlinlang.org/docs/reference/data-classes.html#data-classes"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/971415180415524864"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d34a29ac08f98fc73a5d7f66761fbe45/href">https://medium.com/media/d34a29ac08f98fc73a5d7f66761fbe45/href</a></iframe><h4>8. Visibility Modifiers</h4><p>In Kotlin, everything is public by default! Well, almost. Kotlin has a rich set of visibility modifiers you can use as well: private, protected, internal. Each of them reduces the visibility in a different way. Check the <a href="https://kotlinlang.org/docs/reference/visibility-modifiers.html"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/971777567542988801"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c700d4f7d4f16c89b1852f1920a87e30/href">https://medium.com/media/c700d4f7d4f16c89b1852f1920a87e30/href</a></iframe><h4>9. Default Arguments</h4><p>Is the number of method overloads getting out of hand? Specify default parameter values in functions. Make the code even more readable with named parameters. Check the <a href="https://kotlinlang.org/docs/reference/functions.html#default-arguments"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/972139953995083776"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98ae88681dea504565f9be25e16e356b/href">https://medium.com/media/98ae88681dea504565f9be25e16e356b/href</a></iframe><h4>10. Sealed Classes</h4><p><strong>[1] </strong>Kotlin sealed classes let you easily handle error data. When combined with LiveData you can use one LiveData to represent both the success path and the error path. Way better than using two variables. Check the <a href="https://kotlinlang.org/docs/reference/sealed-classes.html"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/972502799496790018"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5483fd0099eecf98202c4f41f76a3eb8/href">https://medium.com/media/5483fd0099eecf98202c4f41f76a3eb8/href</a></iframe><p><strong>[2] </strong>You can also use sealed classes in a RecyclerView adapter. They’re a perfect fit for ViewHolders — with a clean set of types to dispatch explicitly to each holder. Used as an expression, the compiler will error if all types aren’t matched. Check the original <a href="https://twitter.com/AndroidDev/status/972502799496790018"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/239db887d444665b41701f9b461849cf/href">https://medium.com/media/239db887d444665b41701f9b461849cf/href</a></iframe><p><strong>[3] </strong>Going further with RecyclerViews, if we have a lot of callbacks from a RecyclerView item, like this one with detail clicks, shares, and delete actions, we can use sealed classes. One callback taking one sealed class can handle all the things! Check the original <a href="https://twitter.com/AndroidDev/status/972502802336423936"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d522c1e4b9a05dddf90eb4afc6bce7e2/href">https://medium.com/media/d522c1e4b9a05dddf90eb4afc6bce7e2/href</a></iframe><h4>11. Delegate Properties with Lazy</h4><p>Defer the cost of expensive property initialization until they’re actually needed, by using lazy. The computed value is then saved and used for any future calls. Check the <a href="https://kotlinlang.org/docs/reference/delegated-properties.html#lazy"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/972849635189514240"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1dac28a479578b65ff3bac03e0520c0c/href">https://medium.com/media/1dac28a479578b65ff3bac03e0520c0c/href</a></iframe><h4>12. Late-Initialized Properties and Variables</h4><p>In Android, onCreate or other callbacks initialize objects. In Kotlin non-null vals must be initialized. What to do? Enter lateinit. It’s a promise: initialize me later! Use it to pinky-swear it will “eventually” be null safe. Check the <a href="https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/973217848632819712"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd0dcee3066893b1bac4b8055bb447d1/href">https://medium.com/media/fd0dcee3066893b1bac4b8055bb447d1/href</a></iframe><h4>13. Checking arguments</h4><p><strong>[1]</strong> Are your function arguments valid? Check before using them, with require. If they’re not valid an IllegalArgumentException is thrown. Check the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/973576523180515328"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ad974e3656b5e622dc97f3c17cd8bdc/href">https://medium.com/media/5ad974e3656b5e622dc97f3c17cd8bdc/href</a></iframe><p><strong>[2]</strong> Is the state of your enclosing class correct? Use check to verify. It will throw an IllegalStateException if the value checked is false. Check the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/check.html"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/973576524744998912"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42edd7acc729f87ba97431d03d885486/href">https://medium.com/media/42edd7acc729f87ba97431d03d885486/href</a></iframe><h4>14. Inline Functions</h4><p>Can’t wait to use lambdas to make new APIs? Get in line. Kotlin lets you specify a function as inline — which means calls will be replaced with the function body. Breathe and make lambda-based APIs with zero overhead. Check the <a href="https://kotlinlang.org/docs/reference/inline-functions.html"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/973936795670007808"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c29eae95aaf3b1e6d5a1b66196ac0ad2/href">https://medium.com/media/c29eae95aaf3b1e6d5a1b66196ac0ad2/href</a></iframe><h4>15. Calling Kotlin from Java</h4><p>Using Kotlin and Java in the same project? Do you have classes with top-level functions or properties? By default, the compiler generates the class name as “YourFileKt”. Change it by annotating the file with `<a href="https://twitter.com/file">@file</a>:JvmName(“…“)`. Check the <a href="https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#package-level-functions"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/974299186559684608"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d25d16789b42905d8db7f8bd9d2dba85/href">https://medium.com/media/d25d16789b42905d8db7f8bd9d2dba85/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b59c0167713f96e1207a09dc1eb0eca3/href">https://medium.com/media/b59c0167713f96e1207a09dc1eb0eca3/href</a></iframe><h4>16. Reified type parameters</h4><p>To make the concept of reified concrete an example is in order: Context.systemService() in <a href="https://github.com/android/android-ktx">Android KTX</a> uses reified to pass a “real” type via generics. No more passing classes to getSystemService! Check the <a href="https://kotlinlang.org/docs/reference/inline-functions.html#reified-type-parameters"><strong>Docs</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/974674248596779008"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77a03a553750d8187c5bf2bffcf31005/href">https://medium.com/media/77a03a553750d8187c5bf2bffcf31005/href</a></iframe><h4>17. Delegation</h4><p>Delegate your work to another class with by. Favor composition over inheritance with class delegation and reuse property accessor logic with delegator properties. Check <a href="https://kotlinlang.org/docs/reference/delegation.html"><strong>Delegation</strong></a>,<strong> </strong><a href="http://kotlinlang.org/docs/reference/delegated-properties.html"><strong>Delegated properties</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/975023959837216768"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b597631054173a42b648c82f69f184c/href">https://medium.com/media/7b597631054173a42b648c82f69f184c/href</a></iframe><h4>18. Extensions</h4><p>No more Util classes! Extend the functionality of a class by using `extension functions`. Put the name of the class you’re extending before the name of the method you’re adding. This <a href="https://proandroiddev.com/kotlin-extensions-to-commit-fragments-safely-de06218a1f4">medium story</a> offers a good example to see how extensions can be used 😛. Check the <a href="https://kotlinlang.org/docs/reference/extensions.html#extension-functions"><strong>Docs</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/975388458419023872"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7eb3f33dcf5d3553a33dd428ffac0200/href">https://medium.com/media/7eb3f33dcf5d3553a33dd428ffac0200/href</a></iframe><p>Extension functions:</p><ul><li>Are not member functions</li><li>Do not modify the original class in any way</li><li>Are resolved at compile time by static type information</li><li>Are compiled to static functions in Java.</li><li>Don’t do polymorphism</li></ul><h4>19. KTX Convert a Drawable to a Bitmap</h4><p>If you ever converted a Drawable to a Bitmap then you know how much boilerplate you need. <a href="https://github.com/android/android-ktx">Android KTX</a> has a great set of functions to make your code more concise when working with classes from the graphics package. Check <a href="https://github.com/android/android-ktx/tree/master/src/main/java/androidx/core/graphics/drawable"><strong>Github</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/975758878590709761"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e724fc329641e96856dc803b5d6498de/href">https://medium.com/media/e724fc329641e96856dc803b5d6498de/href</a></iframe><h4>20. Sequences</h4><p><strong>[1]</strong> Sequences are lists that never existed. A Sequence is a cousin of Iterator, lazily generating one value at a time. This matters when using map and filter — they’ll create Sequences instead of copying the list for every step! Check the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/index.html"><strong>Docs</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/976111459955482625"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6441594b15d068d616caaee3f1318573/href">https://medium.com/media/6441594b15d068d616caaee3f1318573/href</a></iframe><p><strong>[2] </strong>You can make sequences from a list or by specifying a next function. If you never terminate a sequence, it can be infinitely long without running out of memory. With coroutines in Kotlin you can also use generators! Check the <a href="https://kotlinlang.org/docs/reference/coroutines.html#generators-api-in-kotlincoroutines"><strong>Docs</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/976111462098771968"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9e9d210e8b5c8945ac93dff39d309d5/href">https://medium.com/media/a9e9d210e8b5c8945ac93dff39d309d5/href</a></iframe><h4>21. Operator overloading</h4><p>Write Kotlin (time * 2) faster with operator overloading. Objects like Path, Range or SpannableStrings naturally allow for operations like addition or subtraction. With Kotlin, you can implement your own operators. Check the <a href="https://kotlinlang.org/docs/reference/operator-overloading.html#operator-overloading"><strong>Docs</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/976473511173738497"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4cf4775b8b3ca6877a3a44c642fa3db/href">https://medium.com/media/a4cf4775b8b3ca6877a3a44c642fa3db/href</a></iframe><h4>22. Basic Syntax</h4><p><strong>[1]</strong> Utility methods for a class? Add them to the top level of the source file. In Java, they are compiled as static methods of that class. Check the <a href="https://kotlinlang.org/docs/reference/basic-syntax.html"><strong>Docs</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/976835899584630785"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c6526328bc0f9b104709fcd24b9dcc4/href">https://medium.com/media/5c6526328bc0f9b104709fcd24b9dcc4/href</a></iframe><p><strong>[2]</strong> Are you defining static constants for your class? Make them top-level properties. They will be compiled to a field and static accessor(s). Check the original <a href="https://twitter.com/AndroidDev/status/976836086122123265"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/621f85baadce72f064f49853a7534458/href">https://medium.com/media/621f85baadce72f064f49853a7534458/href</a></iframe><h4>23. KTX iterators</h4><p>Iterators in interesting places? <a href="https://github.com/android/android-ktx">Android KTX</a> adds iterators to ViewGroup and SparseArray. To define iterator extensions use the `operator` keyword. Foreach loops will use the extensions! Check the <a href="https://kotlinlang.org/docs/reference/control-flow.html#for-loops"><strong>Docs</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/977198285395038214"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03638ad2d9c2ed3920b7586739472ba4/href">https://medium.com/media/03638ad2d9c2ed3920b7586739472ba4/href</a></iframe><h4>24. KTX Content Values</h4><p>Combine the power of Content Values with the brevity of Kotlin. Use the Android KTX Content Values creator and just pass a Pair&lt;StringKey, Value&gt;. Check the <a href="https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/content/ContentValues.kt#L21"><strong>Github</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/977560672354652161"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b20dd1169e547444585360d071914474/href">https://medium.com/media/b20dd1169e547444585360d071914474/href</a></iframe><h4>25. DSL in Kotlin</h4><p><strong>[1] </strong>Domain specific languages can be made by using type safe builders. They make for clean APIs; and you can build them yourself too. Check <a href="https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver"><strong>Lambas</strong></a><strong>, </strong><a href="https://kotlinlang.org/docs/reference/type-safe-builders.html"><strong>Type-Safe Builders</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/977960852342022144"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2ca14a6e262f38cea10cc7b52b999ac/href">https://medium.com/media/d2ca14a6e262f38cea10cc7b52b999ac/href</a></iframe><p><strong>[2] </strong>Spek is a testing library built as a Kotlin DSL. Instead of using `@Annotations`, Spek provides a typesafe way to declare your test code without relying on reflection magic. Check out <a href="http://spekframework.org/"><strong>Speak framework</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/977961342421278720"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adc32ffb23fe97f18d8e45d62206d296/href">https://medium.com/media/adc32ffb23fe97f18d8e45d62206d296/href</a></iframe><p><strong>[3] </strong>Wish the Gradle DSL had static typing? Wish no more! With the Kotlin Gradle DSL, you can build type-safe build files that integrate with the android gradle plugin. Check out `<a href="https://github.com/gradle/kotlin-dsl/blob/master/samples/hello-android/build.gradle.kts">build.gradle.kts</a>` for an example and the original <a href="https://twitter.com/AndroidDev/status/977961710941171712"><strong>Tweet</strong></a>!</p><p><strong>[4] </strong>Another DSL for Kotlin on Android is Anko. Anko lets you build Android views using declarative code. Check out <a href="https://github.com/Kotlin/anko"><strong>Anko</strong></a> and the original <a href="https://twitter.com/AndroidDev/status/977962055322894336"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c7fa21263af80470b64802d96761e79/href">https://medium.com/media/9c7fa21263af80470b64802d96761e79/href</a></iframe><h4>26. KTX Bundles</h4><p>Bundle up, and get ready for the concise bundle creator in <a href="https://github.com/android/android-ktx">Android KTX</a>. No more calls to putString, putInt , or any of their 20 friends. One call will make you a new bundle, and it’ll even handle Arrays! Check the <a href="https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/os/Bundle.kt#L27"><strong>Code</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/978285449822302208"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f910d7fb5d5d2a9c30657cd88ab5d17a/href">https://medium.com/media/f910d7fb5d5d2a9c30657cd88ab5d17a/href</a></iframe><h4>27. Lambdas</h4><p>Lambdas are sweet. With last parameter call syntax, you can cleanup callbacks, Callable, and Runnable. For example, <a href="https://github.com/android/android-ktx">Android KTX</a> sweetens postDelayed with a small wrapper. Check the <a href="https://kotlinlang.org/docs/reference/lambdas.html"><strong>Docs</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/978647840275738625"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0b4f528d7c68b0bf32078a1f2ff46eb/href">https://medium.com/media/b0b4f528d7c68b0bf32078a1f2ff46eb/href</a></iframe><h4>28. KTX Spannable String</h4><p>Powerful but hard to use — that’s how the text styling Spans API feels. <a href="https://github.com/android/android-ktx">Android KTX</a> adds extension functions for some of the most common spans and makes the API easier to use. Check the <a href="https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/text/SpannableStringBuilder.kt"><strong>Code</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/979010225612173312"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/926ecfc6e00a833879f26a4c6b53ff21/href">https://medium.com/media/926ecfc6e00a833879f26a4c6b53ff21/href</a></iframe><h4>29. Parcelable</h4><p>Love the speed of Parcelable, but don’t like writing all that code? Say hello to <a href="https://github.com/Kotlin/KEEP/blob/master/proposals/extensions/android-parcelable.md">Parcelize</a>. Check the original <a href="https://twitter.com/AndroidDev/status/979372612500508673"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ac72d1981926d966b6a24542f7f2e33/href">https://medium.com/media/7ac72d1981926d966b6a24542f7f2e33/href</a></iframe><h4>30. Extending existing APIs</h4><p>Extending existing APIs with default arguments usually makes everyone happy. <a href="https://github.com/android/android-ktx">Android KTX</a> lets you set the padding on one side of a view using default parameters. A one line function that saves so much code! Check the <a href="https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/view/View.kt#L117"><strong>Code</strong></a><strong> </strong>and the original <a href="https://twitter.com/AndroidDev/status/979735000257105922"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92fd9f092498c2df2caa946588fe54d7/href">https://medium.com/media/92fd9f092498c2df2caa946588fe54d7/href</a></iframe><h4>31. Scope functions</h4><p>Let’s run with some standard Kotlin functions! Short and powerful, let, apply, with, also and run all have a receiver (this), may have an argument (it) and may have a return value. See the differences ⬇ and check the original <a href="https://twitter.com/AndroidDev/status/980097396058279937"><strong>Tweet</strong></a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d0723e91b9c9ad9d3ee7031401d5ddb/href">https://medium.com/media/2d0723e91b9c9ad9d3ee7031401d5ddb/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3aff5b631774161ac7bd0121891ced04/href">https://medium.com/media/3aff5b631774161ac7bd0121891ced04/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9409ce4bc0811d2897aafea980cd9175/href">https://medium.com/media/9409ce4bc0811d2897aafea980cd9175/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5326d631d663c79c05a017e0931b85a/href">https://medium.com/media/f5326d631d663c79c05a017e0931b85a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c706f39e753f78f44079afd34cf41c7d/href">https://medium.com/media/c706f39e753f78f44079afd34cf41c7d/href</a></iframe><p>That’s all folks, I hope it has been helpful for you 🖖</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68319d1d4972" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/summary-of-31daysofkotlin-68319d1d4972">Summary of #31DaysOfKotlin</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Deprecated] Kotlin extensions to commit Fragments safely ]]></title>
            <link>https://proandroiddev.com/kotlin-extensions-to-commit-fragments-safely-de06218a1f4?source=rss-116f8e2f3e10------2</link>
            <guid isPermaLink="false">https://medium.com/p/de06218a1f4</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[fragments]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Marc Garcia]]></dc:creator>
            <pubDate>Tue, 15 Aug 2017 09:59:34 GMT</pubDate>
            <atom:updated>2022-01-13T09:38:53.004Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Use the official FragmentManager extension to manage fragments: https://developer.android.com/guide/fragments/transactions</blockquote><p>Since fragments appeared in Android, many developers have criticized this component. The main reasons to not recommend fragments in android are; complex lifecycle, hard to debug and easy to lose the fragment state.</p><p>Regarding this last reason, how many of you have seen this exception in your implementations?</p><pre>java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState<br>    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1341)<br>    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1352)<br>    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)<br>    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)</pre><p>The goal of this post is to show you a way to prevent the state loss fragment exception using kotlin extensions and android support library (version up to 26.0.0).</p><h4>Why is this exception thrown? 🤔</h4><p>If you don’t know why this exception is thrown, I strongly recommend you to read the <a href="http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html"><em>Fragment Transactions &amp; Activity State Loss</em></a> article from <a href="http://twitter.com/alexjlockwood">@alexjlockwood</a>, in which he explains it in great detail.</p><h4><strong>Android Support Library</strong></h4><p>Since <a href="https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0-beta1"><strong>version 26.0.0-beta 1</strong></a> we can check in your app if fragments can be committed to allow the state loss or not.</p><blockquote>FragmentManager and Fragment have an isStateSaved() method to allow querying whether or not a transaction will be allowed without state loss. This is especially useful to check when handling an onClick() event before executing any transaction.</blockquote><p>As you can see, the <em>isStateSaved()</em> method is really useful in order to avoid unexpected scenarios.</p><h4><strong>Kotlin extensions</strong></h4><p><a href="https://kotlinlang.org/docs/tutorials/android-plugin.html"><strong>Kotlin Android Extensions</strong></a> can be really useful to improve support for Android development. <a href="https://kotlinlang.org/docs/reference/extensions.html"><strong>Extensions</strong></a> provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern. Kotlin supports <em>extension functions</em> and <em>extension properties</em>.</p><h4>How to combine both?</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FTbYgHMnICI1A4%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FTbYgHMnICI1A4%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FTbYgHMnICI1A4%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="122" frameborder="0" scrolling="no"><a href="https://medium.com/media/95c560d4bc9acf45420b02cef29092dd/href">https://medium.com/media/95c560d4bc9acf45420b02cef29092dd/href</a></iframe><p>Well, it’s not rocket science. In the following example you can see how an activity extension can commit a fragment in a safe way using <a href="https://developer.android.com/reference/android/app/FragmentManager.html#isStateSaved()"><strong><em>isStateSaved</em></strong></a> method from the android support library:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92dfcb3b5dba0c27a8441f4c28cf4670/href">https://medium.com/media/92dfcb3b5dba0c27a8441f4c28cf4670/href</a></iframe><p>Then, it can be invoked directly from any activity that extends from AppCompatActivity:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84beb4fe2080f4924aeb7eb4287bfd4f/href">https://medium.com/media/84beb4fe2080f4924aeb7eb4287bfd4f/href</a></iframe><h4><strong>Github example</strong></h4><p>I’ve created a simple project example on Github 😊</p><p><a href="https://github.com/mgarciaguerrero/safe-android-fragments"><strong>https://github.com/mgarciaguerrero/safe-android-fragments</strong></a></p><p>That’s all folks, I hope it has been helpful for you 🖖</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de06218a1f4" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/kotlin-extensions-to-commit-fragments-safely-de06218a1f4">[Deprecated] Kotlin extensions to commit Fragments safely 😎</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>