<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dinorah Tovar</title>
    <description>The latest articles on Forem by Dinorah Tovar (@ddinorahtovar).</description>
    <link>https://forem.com/ddinorahtovar</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%2F202188%2F05b734ec-8a0a-47c7-89a4-a3851c354f39.png</url>
      <title>Forem: Dinorah Tovar</title>
      <link>https://forem.com/ddinorahtovar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ddinorahtovar"/>
    <language>en</language>
    <item>
      <title>My app is on background or foreground?</title>
      <dc:creator>Dinorah Tovar</dc:creator>
      <pubDate>Fri, 16 Aug 2019 18:18:36 +0000</pubDate>
      <link>https://forem.com/ddinorahtovar/my-app-is-on-background-or-foreground-44b6</link>
      <guid>https://forem.com/ddinorahtovar/my-app-is-on-background-or-foreground-44b6</guid>
      <description>&lt;p&gt;Solving the problem for OnPause &amp;amp; OnResume using LifecycleObserver&lt;/p&gt;

&lt;p&gt;One of the most asked questions on StackOverflow is something like “How do I know if my app is on background” and we have multiple duplicates like &lt;a href="https://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background"&gt;this one&lt;/a&gt; or this &lt;a href="https://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service"&gt;another one&lt;/a&gt;. &lt;br&gt;
This question has been hunting a lot of android developers from a long time, for some time the Android Developers has been doing this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And a &lt;strong&gt;lot&lt;/strong&gt; of persons are still using a variable that changes depending on the configuration, in my personal opinion, this is not the right approach to solve this issue, let me explain why, on our last project we were using multiple fragments and activities with different flows and navigations, but every time the users goes to another app, maybe open the task manager or the phone gets locked, we need to show the user a “Pin View” it means you are locked from your session till you verify your pin and do a login.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;We have multiple views and multiple fragments and using MyApplication to solve this problem sounds like tons and tons of work. &lt;br&gt;
We thought, maybe if we do a BaseAppCompatActivity we can solve this issue of the multiple OnPause and OnResume, but this was, again, not the right solution. &lt;br&gt;
When you change from one activity to another activity, the Activity A goes to OnPause for a second and we were able to see the Pin View.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;The final solution was based on &lt;a href="https://developer.android.com/topic/libraries/architecture/lifecycle"&gt;LifecycleObserver&lt;/a&gt;, which is part of the LifeCycle Aware Components proposed on Android Jetpack, so we made an ApplicationOwner class that extends from LifecycleObserver and use OnLifecycleEvent to check the “events” inside an Application.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We decide to use Events to check when the app was starting or getting stopped. That helps us a lot, cause the lifecycle events are dispatched from the framework and the Lifecycle class. These events map to the callback events in activities and fragments.&lt;br&gt;
But maybe you are thinking, this is the same problem you have with going from Activity A to Activity B, you are going to see the Pin View when you changed it, well, not exactly, cause we decide to use ProcessLifecycleOwner.&lt;/p&gt;

&lt;p&gt;This class provides lifecycle for the whole application process, and you can implement it like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;You can consider this LifecycleOwner as the composite of all of your Activities, except that ON_CREATE will be dispatched once and ON_DESTROY will never be dispatched. Other lifecycle events will be dispatched with the following rules: ProcessLifecycleOwner will dispatch ON_START, ON_RESUME events, as a first activity moves through these events. ON_PAUSE, ON_STOP, events will be dispatched with a delay after a last activity passed through them. This delay is long enough to guarantee that ProcessLifecycleOwner won't send any events if activities are destroyed and recreated due to a configuration change.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We put this on MyApplication to catch only the application and not all activities. &lt;/p&gt;

&lt;p&gt;For more information please visit: (Handling Lifecycles with Lifecycle-Aware Components)[&lt;a href="https://developer.android.com/topic/libraries/architecture/lifecycle"&gt;https://developer.android.com/topic/libraries/architecture/lifecycle&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;Happy Coding! 👩🏻‍💻&lt;br&gt;
If you have questions, you can reach me here at Dev Community or:&lt;br&gt;
Medium: &lt;a href="https://medium.com/knowing-android"&gt;https://medium.com/knowing-android&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://twitter.com/DDinorahtovar"&gt;https://twitter.com/DDinorahtovar&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>architecture</category>
      <category>background</category>
    </item>
    <item>
      <title>Headers, Interceptors, and Authenticators with Retrofit</title>
      <dc:creator>Dinorah Tovar</dc:creator>
      <pubDate>Fri, 16 Aug 2019 17:16:17 +0000</pubDate>
      <link>https://forem.com/ddinorahtovar/headers-interceptors-and-authenticators-with-retrofit-13ma</link>
      <guid>https://forem.com/ddinorahtovar/headers-interceptors-and-authenticators-with-retrofit-13ma</guid>
      <description>&lt;p&gt;In Android sometimes you need to add a couple of parameters, like headers, to make a successful request, this is normal behavior from all the Android Apps when you are using Retrofit, you can do it in multiple ways&lt;br&gt;
For example, you can add parameters directly to your request interface using the annotation Headers and putting a plain String, like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Another solution is to send the Headers as a parameter to your interface function, using an annotation Header and sending a parameter, this gives you the possibility to have a custom parameter that you can manage from every request:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Interceptor
&lt;/h2&gt;

&lt;p&gt;A couple of people using Dagger probably will go for an Interceptor, you can have two types of interceptor:&lt;br&gt;
The first one is using an interceptor directly in your Singleton, this will not give you versatility, but it will solve your problem faster, in this example, you can go for the chain object, get the request of the Retrofit call, get a new Builder and then add the Headers.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Or you can use this as file apart, extending Interceptor class, this will let you have the reusable code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Authenticator
&lt;/h2&gt;

&lt;p&gt;Imagine you want to catch the response before to go to your Observable, maybe trying to catch a specific error before hitting your Throwable, for this specific behavior you need an Authenticator.&lt;br&gt;
This authenticator will catch the response, when there is an error (401 error) then we will update the AuthToken for the failed request and it will send resend the request, no need to send anything else or make any logic inside your presentation layer.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;To make the implementation you only need to set this object like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And you re ready to go. &lt;br&gt;
Happy Coding! 👩🏻‍💻&lt;br&gt;
If you have questions, you can reach me here at Dev Community or:&lt;br&gt;
Medium: &lt;a href="https://medium.com/knowing-android"&gt;https://medium.com/knowing-android&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://twitter.com/DDinorahtovar"&gt;https://twitter.com/DDinorahtovar&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>retrofit</category>
    </item>
  </channel>
</rss>
