<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rebecca Franks</title>
    <description>The latest articles on DEV Community by Rebecca Franks (@riggaroo).</description>
    <link>https://dev.to/riggaroo</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%2F4179%2Fc1b10dd9-8cad-46fa-8dc8-779fe2f0b386.png</url>
      <title>DEV Community: Rebecca Franks</title>
      <link>https://dev.to/riggaroo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/riggaroo"/>
    <language>en</language>
    <item>
      <title>Dark Mode Musings: Beware of the Context 🌗</title>
      <dc:creator>Rebecca Franks</dc:creator>
      <pubDate>Sat, 14 Dec 2019 08:06:05 +0000</pubDate>
      <link>https://dev.to/riggaroo/dark-mode-musings-beware-of-the-context-3ikk</link>
      <guid>https://dev.to/riggaroo/dark-mode-musings-beware-of-the-context-3ikk</guid>
      <description>&lt;p&gt;I’ve been working on getting Dark Mode in our app fully supported and I stumbled upon an interesting finding:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Application#applicationContext does not keep information about the theme that you have set via AppCompatDelegate.setDefaultNightMode(), only a View or Activity context has this information stored.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After reading through &lt;a href="https://medium.com/androiddevelopers/appcompat-v23-2-daynight-d10f90c83e94"&gt;Chris Banes’ articles&lt;/a&gt; and watching some great talks about &lt;a href="https://www.youtube.com/watch?v=Owkf8DhAOSo"&gt;Styles, Themes &amp;amp; Dark Mode&lt;/a&gt;, I felt pretty comfortable that I knew quite a bit about the Theming system on Android. However, this particular issue was something that I was not expecting, which is why I decided to document this particular problem I faced.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;I have the “Dark theme” set on in my system settings, but inside my app I am explicitly setting the theme to light mode (even though my device theme is set to Dark):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have two bitmap resources: one located in &lt;code&gt;drawable&lt;/code&gt; and one located in &lt;code&gt;drawable-night&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KEwVjpjB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bztyy0n5hwwzux5bucc1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KEwVjpjB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bztyy0n5hwwzux5bucc1.jpg" alt="Different Resources for Images"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Despite my app theme overriding the mode to “light”, the resource from &lt;code&gt;drawable-night&lt;/code&gt; was loaded instead of the one from the &lt;code&gt;drawable&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v6JfX5cX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/75mnimaz4otlalj9znjc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v6JfX5cX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/75mnimaz4otlalj9znjc.jpg" alt="Incorrect Drawable Resource Loaded up for themes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why was this happening? 🧐
&lt;/h2&gt;

&lt;p&gt;I was using the &lt;code&gt;Application#applicationContext&lt;/code&gt; to load up the Bitmap:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class BitmapLoader(val context: Context) {

    override fun loadBitmap(@RawRes resourceId: Int, bitmapConfig: Bitmap.Config): Bitmap? {
        val options =
            BitmapFactory.Options().apply { inPreferredConfig = bitmapConfig }
        return BitmapFactory.decodeResource(
            context.resources, resourceId, options
        )
    }
}
// This class was used in a similar way to the following usage:
val bitmapLoader = BitmapLoader(application.applicationContext)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;The fix in this case was to use the &lt;code&gt;Activity#context&lt;/code&gt; method or &lt;code&gt;View#context&lt;/code&gt;, instead of &lt;code&gt;Application#applicationContext&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oU_LxJ5b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zrj96985pjtuvfwi4u6l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oU_LxJ5b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zrj96985pjtuvfwi4u6l.jpg" alt="Correct background grid loaded up for theme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The lesson here is that the &lt;code&gt;Application#applicationContext&lt;/code&gt; should not be used for retrieving UI resources. The Application object will only have system-level information and not information that is set by &lt;code&gt;AppCompatDelegate&lt;/code&gt;. &lt;code&gt;AppCompatDelegate&lt;/code&gt; only works on the activity-level.&lt;/p&gt;

&lt;p&gt;You need to be very careful of the context you use when obtaining resources that could change based on the theme.&lt;/p&gt;

&lt;p&gt;Thanks for reading, if you have any questions or comments — feel free to reach out on Twitter &lt;a href="https://www.twitter.com/riggaroo"&gt;@riggaroo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks to Chris Banes and Alan Viverette for confirming what I was experiencing is intentional.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>darkmode</category>
      <category>context</category>
    </item>
    <item>
      <title>Nevertheless, Rebecca Franks Coded</title>
      <dc:creator>Rebecca Franks</dc:creator>
      <pubDate>Tue, 07 Mar 2017 06:38:39 +0000</pubDate>
      <link>https://dev.to/riggaroo/nevertheless-rebecca-franks-coded</link>
      <guid>https://dev.to/riggaroo/nevertheless-rebecca-franks-coded</guid>
      <description>&lt;h2&gt;
  
  
  I began coding because...
&lt;/h2&gt;

&lt;p&gt;I was curious. I wanted a job where I could combine maths and computers. I chose a degree that suited that without &lt;em&gt;any&lt;/em&gt; knowledge of how to program. It was tough, I spent a lot more time than everyone else reading as much as I could in order to have it make sense to me. The first 6 months of university were the hardest, I couldn't cope as well as the others. I had countless thoughts of quitting but instead I kept at it until it made sense.  &lt;/p&gt;

&lt;h2&gt;
  
  
  I'm currently hacking on...
&lt;/h2&gt;

&lt;p&gt;Many things, mostly learning about Android Things and electronics. I'm a real electronics newbie, so I am learning a lot as I go and blogging about it on &lt;a href="https://riggaroo.co.za"&gt;https://riggaroo.co.za&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  I'm excited about...
&lt;/h2&gt;

&lt;p&gt;The future of technology. I cant wait for driverless cars to become popular. I believe there is still so much to be discovered and made. I really want more innovations with the food industry to come about, things such as ovens that can bake things themselves and know when food is ready. &lt;/p&gt;

&lt;h2&gt;
  
  
  My advice for other women who code is...
&lt;/h2&gt;

&lt;p&gt;It gets easier! Not easier as in &lt;em&gt;"I understand everything about coding and I know it all"&lt;/em&gt;, it gets easier as in &lt;em&gt;"I don't know how to solve this problem but I am confident I can figure it out"&lt;/em&gt;. The truth is you will never know everything there is to know, there will always be a new language, a new platform, a new templating engine. It is impossible to know &lt;em&gt;everything&lt;/em&gt;. Most importantly just keep going. Make sure you learn a new skill everyday. Encourage others and never forget that once upon a time you also had no clue how to code. &lt;/p&gt;

</description>
      <category>wecoded</category>
    </item>
  </channel>
</rss>
