<?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 Rajat Negi on Medium]]></title>
        <description><![CDATA[Stories by Rajat Negi on Medium]]></description>
        <link>https://medium.com/@rajatnegi?source=rss-ff0df0c79557------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*NMVMgIXd_pinC594_Ux0CA.jpeg</url>
            <title>Stories by Rajat Negi on Medium</title>
            <link>https://medium.com/@rajatnegi?source=rss-ff0df0c79557------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 00:32:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajatnegi/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[Best way to handle network status in Android]]></title>
            <link>https://rajatnegi.medium.com/best-way-to-handle-network-status-in-android-6015b30d4f3d?source=rss-ff0df0c79557------2</link>
            <guid isPermaLink="false">https://medium.com/p/6015b30d4f3d</guid>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[networkinginandroid]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Rajat Negi]]></dc:creator>
            <pubDate>Thu, 27 Feb 2020 07:24:48 GMT</pubDate>
            <atom:updated>2021-04-22T15:03:13.175Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*_rI3RKWoteJ6ynCQOixm_g.png" /></figure><p>Networking is the essential part of android. Most of Android Applications need data in order to interact with user and these data changes dynamically by network calls.</p><h4>Problem</h4><p>The below code is showing how we mostly handle the network calls.The problem with the below code is that when we handle different network calls then this code become messy as we have to add more operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g8so61wcDehhCuZCyNLunQ.png" /></figure><h3>Solution</h3><p>In order to prevent the above we are going to use <strong>LiveData</strong>,<strong>ViewModel</strong> and Resource class that encapsulate both the data and its state which result in clean code.</p><h4>Resource</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KYLo8xjpVyKKuUSLxX5_IQ.png" /></figure><h4>View Model</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-nziLGF0krfx-0QsbCzn-w.png" /></figure><h4><strong>MainActivity</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VVUtvQqMoLOI9xhaCl-YKg.png" /></figure><p>This is the most efficient way to handle network calls and also it is recommended by Google.</p><p>for more information you can go to <a href="https://developer.android.com/jetpack/docs/guide#addendum">https://developer.android.com/jetpack/docs/guide#addendum</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6015b30d4f3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[StopWatch using RxKotlin and RxBinding]]></title>
            <link>https://rajatnegi.medium.com/stopwatch-using-rxkotlin-and-rxbinding-64ccf7b76186?source=rss-ff0df0c79557------2</link>
            <guid isPermaLink="false">https://medium.com/p/64ccf7b76186</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[rxkotlin]]></category>
            <category><![CDATA[rxbindings]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Rajat Negi]]></dc:creator>
            <pubDate>Tue, 03 Dec 2019 09:50:38 GMT</pubDate>
            <atom:updated>2019-12-03T09:50:38.693Z</atom:updated>
            <content:encoded><![CDATA[<h4>Prerequisites</h4><ul><li>Basic knowledge of Rx Java or Rx Kotlin is must.</li><li>MVVM Design Pattern</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*kq3w8S82IYyF7s9Wcis0Eg.png" /><figcaption>Stop watch in<strong> Idle state</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*0htXtMrqMCKYBNE5CuSf8A.png" /><figcaption>pause state</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*pBjgIBgEwlTqSZIA_jr8OQ.png" /><figcaption>Running state</figcaption></figure><p>In this project we have used different Rx Kotlin operators:</p><ul><li>Publish and Behavioral Subject.</li><li>merge()</li><li>hide()</li><li>map()</li><li>interval()</li></ul><h4>Merge Operator</h4><p>Merge operator combine multiple observables into one by merging their emissions.It will not maintain the order while emitting the items.<br>In the below example we are merging the emissions of the buttons(<strong><em>start,pause,reset</em></strong>) using Rxbinding by Jake Wharton.</p><pre><em>listOf</em>&lt;Observable&lt;StopWatchState&gt;&gt;(<br>    start.<em>clicks</em>(),<br>    pause.<em>clicks</em>(),<br>    reset.<em>clicks</em>()).<em>merge</em>()<br>    </pre><h4>hide()</h4><p>In simple word<strong><em> hide</em></strong> operator hides the identity of the <strong><em>Subject </em></strong>i.e<strong><em>.</em></strong>Allows <strong>hiding</strong> extra features such as Subject’s Observer methods or preventing certain identity-based optimizations (fusion).<br>In the given example below the <strong><em>hide() operator changes the Publish Subject<br>from Observer to Observable.</em></strong></p><pre>class MyViewModel : ViewModel(){</pre><pre>  var publish = PublishSubject.create&lt;StopWatchState&gt;()<br>  private val pause = BehaviorSubject.create&lt;Long&gt;()<br>  private val resume = BehaviorSubject.createDefault(0L)</pre><pre>  fun uiStates(): Observable&lt;StopWatchTime&gt; =<br>      publish.switchMap <strong>{ </strong>it <strong>-&gt;<br>          </strong>when (it) {<br>              StopWatchState.IDLE -&gt; {<br>                  Observable.just(StopWatchTime())<br>                      .doOnNext<strong>{</strong>stopWatchTime.time = <strong>it</strong>.time<strong>}<br>                      </strong>.doOnNext <strong>{ </strong>pause.onNext(<strong>it</strong>.time) <strong>}<br>                      </strong>.doOnNext <strong>{ </strong>resume.onNext(<strong>it</strong>.time) <strong>}<br>              </strong>}<br>            StopWatchState.RUNNING -&gt; {<br>                  startTimer()<br>                      .doOnNext(pause::onNext)<br>                      .map <strong>{ </strong>sec <strong>-&gt; </strong>StopWatchTime(sec,   StopWatchState.RUNNING) <strong>}<br>              </strong>}<br>              StopWatchState.PAUSE-&gt; {<br>                  resume.onNext(pause.<em>value</em>!!)<br>                  Observable.just(<br>                      StopWatchTime(<br>                          pause.<em>value</em>!!,<br>                          state = StopWatchState.PAUSE<br>                      )<br>                  )<br><br>              }  <br>          }<br>      <strong>}</strong>.hide()<br>//hide operator changes the Subject from observer to observable</pre><pre>  }</pre><h4>map()</h4><p>The Map operator applies a function of your choosing to each item emitted by the source Observable, and returns an Observable that emits the results of these function applications.</p><pre>val observable = Observable.just(&quot;Apple&quot;)<br><br>observable.map <strong>{ it</strong>.length <strong>}<br>    </strong>.subscribeOn(Schedulers.io())<br>    .subscribe <strong>{ </strong><em>println</em>(&quot;$<strong>it</strong>&quot;) <strong>}</strong></pre><p>The output of the above code will be <strong>5</strong>. As in the above code an observable emits a String =&gt;<strong><em>Apple</em></strong> .After than map operator handles the emission and transform it to the Integer by <strong><em>it.length </em></strong>and at last print the result.</p><h4><strong><em>interval()</em></strong></h4><p>it creates an Observable that emits a sequence of integers spaced by a given time <strong>interval</strong>. It is used when we want to do a task again and again after some <strong>interval</strong>.</p><pre>Observable.interval(0, 1, TimeUnit.SECONDS)<br>          .subscribeOn(Schedulers.io())<br>          .observeOn(AndroidSchedulers.mainThread())<br>          .subscribe{textView.setText(it)} </pre><h4>PublishSubject</h4><p>It emits all the subsequent items of the source Observable at the time of subscription.</p><p>Here, if a student entered late into the classroom, he just wants to listen from that point of time when he entered the classroom. So, Publish will be the best for this use-case.</p><pre>PublishSubject&lt;Integer&gt; source = PublishSubject.create();</pre><pre><em>// It will get 1, 2, 3, 4 and onComplete</em><br>source.subscribe(getFirstObserver()); </pre><pre>source.onNext(1);<br>source.onNext(2);<br>source.onNext(3);</pre><pre><em>// It will get 4 and onComplete for second observer also.</em><br>source.subscribe(getSecondObserver());</pre><pre>source.onNext(4);<br>source.onComplete();</pre><p><strong>Behavior Subject</strong></p><p>It emits the most recently emitted item and all the subsequent items of the source Observable when an observer subscribes to it.</p><p>Here, if a student entered late into the classroom, he wants to listen the most recent things(not from the beginning) being taught by the professor so that he gets the idea of the context. So, here we will use Behavior.</p><pre>BehaviorSubject&lt;Integer&gt; source = BehaviorSubject.create();</pre><pre><em>// It will get 1, 2, 3, 4 and onComplete</em><br>source.subscribe(getFirstObserver());</pre><pre>source.onNext(1);<br>source.onNext(2);<br>source.onNext(3);</pre><pre><em>// It will get 3(last emitted)and 4(subsequent item) and onComplete</em><br>source.subscribe(getSecondObserver());</pre><pre>source.onNext(4);<br>source.onComplete();</pre><p>You can find out the whole project at</p><p><a href="https://github.com/RAJATNEGI/RxStopWatch">https://github.com/RAJATNEGI/RxStopWatch</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64ccf7b76186" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Picture In Picture Mode(Android Development)]]></title>
            <link>https://rajatnegi.medium.com/picture-in-picture-mode-android-development-13291afc5841?source=rss-ff0df0c79557------2</link>
            <guid isPermaLink="false">https://medium.com/p/13291afc5841</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Rajat Negi]]></dc:creator>
            <pubDate>Sat, 16 Nov 2019 16:41:54 GMT</pubDate>
            <atom:updated>2019-11-16T16:45:31.464Z</atom:updated>
            <content:encoded><![CDATA[<p>Google has introduced new feature for Android with the name Picture in Picture Mode(PIPM).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*wpNETcuwEwpqoHefsLvTHA.png" /></figure><p>Minimum requirement for this Feature to work in the Android device are: Android 8.0(API level 26)</p><p>PIP is special type of multi window mode which is use mainly for video playback. It let the user watch a video while navigating to other apps on the main screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fy6BxTp90M3FfMtRK1l7_g.jpeg" /></figure><p>Steps to add PIP Mode into your App:<br>1. By default, Android OS does not automatically support PIP for apps. In order to add the support for PIP you have to go to <strong><em>manifest</em></strong> file and register your activity by setting android:supportsPictureInPicture and android:resizeableActivity to true.</p><pre>&lt;activity android:name=&quot;NavigationActivity&quot;<br>    android:resizeableActivity=&quot;true&quot;<br>    android:supportsPictureInPicture=&quot;true&quot;<br>    android:configChanges=<br>        &quot;screenSize|smallestScreenSize|screenLayout|orientation&quot;/&gt;</pre><p>2.After configuring the activity in manifest.xml.<br>Now its time to switch activity to PIP Mode.<br>In order to start PIP Mode on a event an activity should call <strong><em>enterPictureInPictureMode().</em></strong></p><pre>override fun onActionClicked(action: Action) {<br>    if (action.id.toInt() == R.id.lb_control_picture_in_picture) {<br>        activity?.enterPictureInPictureMode()<br>        return<br>    }<br>}</pre><p>3.In your app, a user might select a new video when browsing for content on the main screen, while a video playback activity is in PIP mode. Play the new video in the existing playback activity in full screen mode, instead of launching a new activity that might confuse the user.</p><p>To ensure a single activity is used for video playback requests and switched into or out of PIP mode as needed, set the activity’s android:launchMode to singleTask in your manifest:</p><pre>&lt;activity android:name=&quot;VideoActivity&quot;<br>    ...<br>    android:supportsPictureInPicture=&quot;true&quot;<br>    android:launchMode=&quot;singleTask&quot;<br>    ...</pre><p>In your activity, override <a href="https://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)">onNewIntent()</a> and handle the new video, stopping any existing video playback if needed.</p><h3>Handling UI during Picture In Picture Mode</h3><p>When the activity enters or exits picture-in-picture mode the system calls <a href="https://developer.android.com/reference/android/app/Activity.html#onPictureInPictureModeChanged(boolean,%20android.content.res.Configuration)">Activity.onPictureInPictureModeChanged()</a> or <a href="https://developer.android.com/reference/android/app/Fragment.html#onPictureInPictureModeChanged(boolean,%20android.content.res.Configuration)">Fragment.onPictureInPictureModeChanged()</a>.</p><p>You should override these callbacks to redraw the activity’s UI elements. Keep in mind that in PIP mode your activity is shown in a small window. Users cannot interact with UI elements when in PIP mode and the details of small UI elements may be difficult to see. Video playback activities with minimal UI provide the best user experience. The activity should only show video playback controls. Remove other UI elements before your activity enters PIP and restore them when your activity becomes full-screen again.</p><pre>override fun onPictureInPictureModeChanged<br>(isInPictureInPictureMode: Boolean,newConfig: Configuration) {<br>    if (isInPictureInPictureMode) {<br>        // Hide the full-screen UI (controls, etc.) while in picture-in-picture mode.<br>    } else {<br>        // Restore the full-screen UI.<br>    }</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13291afc5841" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>