<?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 Heming Liu on Medium]]></title>
        <description><![CDATA[Stories by Heming Liu on Medium]]></description>
        <link>https://medium.com/@liuhm1113?source=rss-c9f97f8a7480------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kUy7sTLgAKeMgL6G24Zkmw.jpeg</url>
            <title>Stories by Heming Liu on Medium</title>
            <link>https://medium.com/@liuhm1113?source=rss-c9f97f8a7480------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 10:54:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@liuhm1113/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[Client-Side Ad Insertion — Domain Driven Design And Model-driven Architecture]]></title>
            <link>https://code.tubitv.com/client-side-ad-insertion-domain-driven-design-and-model-driven-architecture-16411f5d6cf5?source=rss-c9f97f8a7480------2</link>
            <guid isPermaLink="false">https://medium.com/p/16411f5d6cf5</guid>
            <category><![CDATA[clients]]></category>
            <category><![CDATA[architectural-design]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Heming Liu]]></dc:creator>
            <pubDate>Mon, 27 Apr 2020 05:09:31 GMT</pubDate>
            <atom:updated>2020-05-06T22:52:44.105Z</atom:updated>
            <content:encoded><![CDATA[<h3>Client-Side Ad Insertion — Domain-Driven Design and Model Driven Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1AXK_Zrhorvti_UV4P7P3w.jpeg" /></figure><p>Ad insertion technology has been a popular tool to help content publishers monetize content and help users enjoy content in a free way. At Tubi, we use client-side ad insertion to deliver advertisement-based video on demand to our users. After the addition of several new features, it resulted in an increased complexity in our playback system; we began to re-think and re-design our whole client-side ad insertion solution to make our application simpler, more maintainable, and scalable, by utilizing domain-driven design and model-driven architecture. This client-side ad insertion design is applicable to many client platforms like iOS, Android, and Web.</p><h3>What is client-side ad insertion?</h3><p>First, let’s take a look at the basic concepts of client-side ad insertion:</p><p>In a video model class, we include some predefined ad play positions to signify when the client can insert and play the ads.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d069cdaa8c697eba92d730ad477cd479/href">https://medium.com/media/d069cdaa8c697eba92d730ad477cd479/href</a></iframe><p>The video player on the client-side, like AVPlayer on iOS, ExoPlayer on Android, or &lt;video&gt; tag on HTML5, plays this video URL. When one of the predefined ad playback positions, defined in adPlayPositionList, is reached, clients pause the video, change playback content and play ads.</p><p>It’s simple so far, but in the real world features and business requirements could add a substantial amount of extra business logic to make playback a fairly complicated system. Below are some functionalities we support at Tubi:</p><ul><li>Fetching ads dynamically a couple of seconds prior to the ad play positions.</li><li>Some ad play positions might not be filled by backend — sometimes no content switching.</li><li>Displaying count-down before ads and during ads</li><li>Automatically skipping ads in the event of ad playback issues.</li><li>Fetching the next video and presenting the new content for the user to continue watching.</li><li>Users exiting at any point during either content or ads, and being able to resume the previous playback.</li><li>Supporting our extensive analytics and performance tracking for our playback experience.</li><li>Allowing for the flexibility to run experiments and add additional features in the future.</li></ul><p>Just like most other software, our playback system is becoming increasingly more complicated:</p><ul><li>Having average twelve different conditional checks when playback states change</li><li>Maintaining nine different player running states, for example, fetching preroll state, playing content state, fetching midroll state, etc.</li><li>Splitting content switching logic into six classes and five files</li><li>Hard-coding fixed values and duplicating logic in several places</li></ul><h3>A Simple And Scalable Design</h3><p>So we began thinking: what can we do to make the whole system simpler? If content switching makes everything more complicated, can we simplify it by just playing one video content each time without constantly worrying about content switching?</p><p>To answer that question, inspired by the domain-driven design paradigm, we wanted to focus our design around a model of our core problem domain. To do this, we summarized the required business logic for ad-insertion then created a domain model to make expressing that logic very easy:</p><ul><li><strong>The video is split into segments by those predefined insertion positions. Ads (which are separate video segments) are then inserted into the content video segments.</strong></li><li><strong>The video player plays video segments in order one by one.</strong></li><li><strong>Content switching only happens at the end of each video segment.</strong></li></ul><p>Let’s look at an example here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*ZQF9B9SMb6ilmSllo8sgQw.jpeg" /><figcaption>An example of how ads and content are stitched together</figcaption></figure><p>For continuous playback experience, we stitch video A and video B together and dynamically insert ads into the ad positions. In the above example, the third ad position for video A and the second ad position for video B have no ads.</p><p>We follow this domain model and build our core models:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c44d37b77920070f28127c2f8f982da3/href">https://medium.com/media/c44d37b77920070f28127c2f8f982da3/href</a></iframe><p>Then we can translate the example to our models:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*NKxC8OYC42y9zzzZrERfzw.jpeg" /></figure><p>Next, we build a client player to play those PlayItems in the list one by one.</p><ul><li>The player launches separate handlers for each type of PlayItem</li><li>If no interruption comes from user input, the player continues playing until the end of current PlayItem</li><li>At the end of current PlayItem, the player grabs the next PlayItem from models (PlayerModel)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*o5abc1F7xjceFwOPPxlHwA.jpeg" /></figure><p>In the meantime, all the fetching and model changes are handled automatically in the background based on current PlayItem. By following the domain model, this system is much simpler than a bunch of conditional checks and logic during playback:</p><p><strong>Each time or any moment the core player and playback logic only need to worry about one thing: playing ONE PlayItem — the current one!</strong></p><h3>It is Also a Model Driven System</h3><p>It was mentioned before that the ads and content stitching at Tubi are highly dynamic: we do not fetch ads or next content until the last couple of seconds from the end of the current video segment — the PlayItem.</p><p>This means our PlayItem playlist is dynamic and could change at any moment.</p><p>To simplify the whole system, we make our models, the PlayItem playlist and the current PlayItem, be the single source of truth to drive all logic:</p><ol><li>The current PlayItem represents what is currently playing</li><li>The playlist represents which PlayItem will be played next</li></ol><p>Our core playback logic class, PlayerHandler, does not maintain the state machine. So we can avoid dealing with scattered conditional checks and states in logic classes, which are centralized and maintained in simple models:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*8y73lTu6N5yk-vlYqs8Spw.jpeg" /></figure><p>We make all operations on the PlayItem playlist atomic. At all times, the playlist represents the current player status and drives the whole playback system. With this model driven design, we are able to easily split the whole system into several independent modules and quickly scale the development. Also, during debugging, because models are the single source of truth, we can easily look up models to locate the issues in self-isolated modules.</p><p>When we add new features in the future, as long as the core domain model is the same, our playback system can be easily extended and scaled.</p><h3>What’s Next?</h3><p>So far, we have been able to implement this design and build our new client-side ad insertion playback systems on both iOS and Android. Here are some major improvement on our production metrics:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6NbOS268Y_nNtd1gciWMNQ.png" /></figure><p>If you are also interested in using hands-on coding skills to solve real world challenges, come <a href="https://tubitv.com/static/careers">join us</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16411f5d6cf5" width="1" height="1" alt=""><hr><p><a href="https://code.tubitv.com/client-side-ad-insertion-domain-driven-design-and-model-driven-architecture-16411f5d6cf5">Client-Side Ad Insertion — Domain Driven Design And Model-driven Architecture</a> was originally published in <a href="https://code.tubitv.com/">Tubi Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS Style Tab Navigation On Android — Equivalent Of UITabBarController]]></title>
            <link>https://code.tubitv.com/ios-style-tab-navigation-on-android-equivalent-of-uitabbarcontroller-d4f28798c170?source=rss-c9f97f8a7480------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4f28798c170</guid>
            <category><![CDATA[fragments]]></category>
            <category><![CDATA[tab-navigation]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Heming Liu]]></dc:creator>
            <pubDate>Mon, 29 Jul 2019 05:10:16 GMT</pubDate>
            <atom:updated>2019-08-12T20:32:10.304Z</atom:updated>
            <content:encoded><![CDATA[<h3>Painless Tab Navigation On Android — Equivalent of iOS UITabBarController</h3><p>Mobile navigation was ruled by the hamburger menu until WWDC in 2014, when Apple’s design team decided to go all-in against this UX pattern. It went viral and inspired publications like TechCrunch to publish <a href="https://techcrunch.com/2014/05/24/before-the-hamburger-button-kills-you/">Kill the Hamburger Button Before It Kills You</a>. Apple put their money where their mouth is and created the <a href="https://developer.apple.com/documentation/uikit/uitabbarcontroller">UITabBarController</a> as part of UIKit to make the switch easier for developers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*xRGaSDoHM_XBdyoxs2wNTA.jpeg" /><figcaption>Apple (circa 2014)</figcaption></figure><p>But change for Android hasn’t been so easy. Without official Android support, there’s no Android equivalent of the UITabBarController, making it difficult to build a smooth main tab navigation experience for Android developers. At Tubi, we place a lot of value on our customer having a smooth experience that’s consistent across platforms. As a result, we decided to build our own tools to make it easy to build smooth tab navigation on Android.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*73tn7GyXob-xH7j6DXhntA.jpeg" /></figure><h4>Let’s summarize the benefits of iOS style tab navigation on Android</h4><ol><li>It provides several individual navigation back stacks besides the main app navigation back stack, so users can have several separate navigation paths and histories.</li><li>With one tap on the current tab icon, it can pop all the way back to the first view, providing a way for users to reset nested navigation history.</li><li>Sometimes we want to align the user experience on Android with the user experience on iOS.</li><li>It could provide badging to communicate with users unobtrusively.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*NzA2FjmJRlPhQNANzu459A.png" /><figcaption>iOS tab navigation uses a tab bar to organize information at the app level</figcaption></figure><h4>It’s hard to build tab navigation from scratch on Android</h4><ol><li>There is no official or popular solution on Android, so developers will have to use low level Android UI APIs.</li><li>Android views can be recreated frequently due to any configuration changes like screen rotation, Activity recreation, etc. Maintaining multiple navigation histories efficiently is challenging.</li><li>Managing multiple tabs and finding which tab to load next views are tricky.</li><li>Android has a “physical” back button. Popping view correctly in multiple navigation back stacks is not an easy thing.</li></ol><h3>The Basic Idea</h3><p>We’ve invested significant resources in managing Fragments in the Tubi app. To make it easy to build a tab navigation system, we were able to advantage of the past work we’ve done in managing back stacks.</p><p>We use FragmentTabHost to set up tabs and use Fragment child FragmentManager to provide separate back stack for each tab. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*IxEK9adzJAwDDCUSp_dfDA.jpeg" /><figcaption>When HomeTab is selected, it will load HomeContainerFragment; When SearchTab is selected, it will load SearchContainerFragment</figcaption></figure><p>For a simple app with two tabs: HomeTab and SearchTab, we load HomeContainerFragment to the HomeTab and load SearchContainerFragment to the SearchTab.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9509c587eeb6493179cb54325fea3be1/href">https://medium.com/media/9509c587eeb6493179cb54325fea3be1/href</a></iframe><p>To build an individual back stack for each tab, we load fragments to the child FragmentManager of each tab container fragment. For our HomeTab example, it would look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bff538eefbc32fdcb8a15a2b4a60bd69/href">https://medium.com/media/bff538eefbc32fdcb8a15a2b4a60bd69/href</a></iframe><h3>The Devil’s in the Details</h3><p>By utilizing fragments child FragmentManager, we add an individual back stack to each tab so we can simulate the similar iOS tab navigation. But to make the UI navigation easy for developers to use and ready for production, there are several extra things you will have to solve:</p><ol><li>When you load a new fragment, since you have multiple back stacks, how does the UI system know which back stack this fragment should be loaded to? Do you have to explicitly tell a fragment instance where to load every time?</li><li>After figuring out which back stack to use, if it’s a different tab than the current tab, how do we automatically switch to the correct tab and load the new fragment?</li><li>FragmentTransaction.commit() only schedules a commit of this transaction, meaning the operation doesn’t happen right away. Since there are several FragmentManagers are involved in this solution, you’ll need to carefully handle some race conditions and several different errors that could be caused by the fragment lifecycle. For example, there are many cases that could cause fragments to be recreated and you will easily get IllegalStateException crashes when you try to commit transactions in the wrong states.</li><li>What about Android back button? Ideally after popping all fragments in the current back stack, it should be able to automatically pop to the previous tab.</li></ol><h3>FragmentOperator to the Rescue</h3><p>To solve those issues, at Tubi we use tagging and annotations to help us manage fragment back stacks. Here is an introduction:</p><p><a href="https://code.tubitv.com/manage-android-fragment-backstack-through-tags-55a0ec3a517e">Managing Android Fragment Backstack Through Tags</a></p><p>At Tubi, we have created an open-source library called FragmentOperator to help you achieve the same UI functionality equivalent to iOS UITabBarController easily on Android:</p><p><a href="https://github.com/Tubitv/FragmentOperator">Tubitv/FragmentOperator</a></p><p>By setting up FragmentOperator and using TabChildFragment annotation, you can easily tell the FragmentOperator which back stack the fragment should be loaded to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc2cad123989732a5c5b077743965906/href">https://medium.com/media/fc2cad123989732a5c5b077743965906/href</a></iframe><p>FragmentOperator automatically switches to the correct tab when it loads a new fragment and when a user taps back button. It also handles errors for many different cases, especially those that are caused by “async” fragment transactions and view lifecycle changes.</p><h3>Tip of the Iceburg</h3><p>Over the past few months we’ve significantly upgrade our content discovery experience on the Tubi Android app, but this is just the beginning. As we grow, we’ll be investing a lot more into building our own tools to create smooth, high quality UX and simultaneously make our team more productive. If you’re a mobile developer and that sounds appealing to you, <a href="https://tubitv.com/static/careers?source=post_page---------------------------">join us</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4f28798c170" width="1" height="1" alt=""><hr><p><a href="https://code.tubitv.com/ios-style-tab-navigation-on-android-equivalent-of-uitabbarcontroller-d4f28798c170">iOS Style Tab Navigation On Android — Equivalent Of UITabBarController</a> was originally published in <a href="https://code.tubitv.com/">Tubi Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing Android Fragment Backstack Through Tags]]></title>
            <link>https://code.tubitv.com/manage-android-fragment-backstack-through-tags-55a0ec3a517e?source=rss-c9f97f8a7480------2</link>
            <guid isPermaLink="false">https://medium.com/p/55a0ec3a517e</guid>
            <category><![CDATA[fragments]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Heming Liu]]></dc:creator>
            <pubDate>Tue, 04 Sep 2018 23:10:13 GMT</pubDate>
            <atom:updated>2018-11-02T06:41:03.395Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MQvJobIzR3LYScHhP5wt5A.png" /></figure><p>Android introduced <a href="https://developer.android.com/guide/components/fragments">Fragments</a> in order to support better view navigation across a wide variety of screen sizes. While Fragment adoption is widespread, handling the backstack is <a href="https://medium.com/square-corner-blog/advocating-against-android-fragments-81fd0b462c97">not always easy</a>. In our experience, managing the backstack is especially complex in situations that deviate from framework defaults, such as custom backstack popping. At <a href="http://tubi.tv/">Tubi</a>, we’ve created a small open-source utility called <a href="https://github.com/Tubitv/FragmentOperator">FragmentOperator</a> for a common set of backstack management problems. In this article, I’ll take you through a few <a href="https://kotlinlang.org/">Kotlin</a>-based examples on when and how to use FragmentOperator to make your life as an Android developer a little easier.</p><h4>Tag, You’re It</h4><p>Google recommends using FragmentTransaction to manipulate fragments. These transactions are then added to the backstack, where later, the operations can be reversed. In order to make custom backstack popping easier, we just need to identify and group some of the fragments so that they can be manipulated out of sequence. To keep things simple, we chose <em>tags</em> to achieve this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d979161dd4f58ea28adb1c0482a8a26/href">https://medium.com/media/7d979161dd4f58ea28adb1c0482a8a26/href</a></iframe><p>The above block of code is almost identical to Google’s <a href="https://developer.android.com/guide/components/fragments">official developer documentation</a> except for the following:</p><ul><li>We use <em>fragmentTag</em> to tag the fragment instance, so that later on we can find the fragment instance with findFragmentByTag.</li></ul><pre>fragmentTransaction.replace(containerId, fragment, fragmentTag)</pre><ul><li>The transaction also needs to be tagged correctly when it is added to the backstack to support using the tag with <em>popBackStack</em>.</li></ul><pre>fragmentTransaction.addToBackStack(fragmentTag)</pre><p>In our library, to make it easier to look up and reference, we use the same tag for both the fragment instance and transaction. Note that each tag is unique for each fragment instance, so that we can manipulate multiple instances of the same class. Also, the library maintains and manages each tag for you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/541da58b165bf43c50f23f21ff0a5edc/href">https://medium.com/media/541da58b165bf43c50f23f21ff0a5edc/href</a></iframe><p>The above is part of the base Fragment class we created for all our fragments. Because the fragment can be recreated when Activity configuration changes (hashCode will be different), we only create “fragmentTag” once and save it in the bundle. We use getFragmentTag() to get the unique tag per fragment instance for fragment transaction.</p><h4>Auto-skipping Fragments on Pop</h4><p>The power of tagging fragments can be demonstrated using a simple example. Imagine we have a simple shopping app with a guest mode. After fresh install, a user lands on home screen (HomeFragment), where he or she can browse items without signing up or signing in. Once the user taps an item to check its details, we show an authentication screen (AuthFragment) to ask the user to sign up or sign in to move forward. Once the user successfully signs up, we display item detail screen (DetailFragment). Let’s take a look at what’s in Fragment backstack:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*s7QlpALCgi5afHF2VafMZQ.jpeg" /></figure><p>Next, the user is done with this item and taps phone back button to navigate back. Normally when Android FragmentManager popBackStack() is called, it will pop to the authentication screen (AuthFragment) first. Hmm, this is not ideal. Since the user already signed up, we don’t want to show him or her the authentication screen again. For a better user experience, we want the user to go from the item detail screen (DetailFragment) directly and smoothly to home screen (HomeFragment), with one tap on phone back button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*WDAfAjVmLQek9EwNuTNHPg.jpeg" /></figure><p>One option would be call popBackStack() twice, but visually it looks bad. And while in this case we only need to skip one Fragment instance (AuthFragment), what if there are multiple fragments that need to be skipped? We definitely don’t want to call popBackStack() multiple times, all at once. So it would be really nice if this can be handled automatically.</p><p>Android FragmentManager actually provides a way to let us skip some Fragment instances when popping backstack:</p><pre>popBackStack(String name, int flags)</pre><p>If we mark AuthFragment instance and save the tag for HomeFragment instance, we can skip AuthFragment instance or any Fragment instances that already exist in the backstack, and navigate to home screen directly. So the key here is to maintain and store the correct tag pointing to the correct Fragment instance.</p><p>In FragmentOperator, we maintain a previous fragment tag of each instance in our base Fragment, which acts as a reference to the destination that we should pop or navigate to when popping the Fragment backstack:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8e224a0f80a15834605dd4a41b58dfa/href">https://medium.com/media/f8e224a0f80a15834605dd4a41b58dfa/href</a></iframe><p>When we load a new Fragment instance, based on the type of new Fragment instance, we save the previous Fragment instance tag to new Fragment instance. If a Fragment instance is marked as skipOnPop, it will maintain a tag pointing to the last Fragment instance that should not be skipped. And it will pass the tag to the next Fragment instance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9747fcfd6dbe57377478550b28741902/href">https://medium.com/media/9747fcfd6dbe57377478550b28741902/href</a></iframe><p>When popBackStack is called, by using the tag pointing to last non-skip Fragment instance, we can easily customize backstack pop behavior and skip any Fragment instances in the backstack.</p><p>In some cases, we want to ensure that we load at most one instance for the same Fragment in the backstack. For example, back to our shopping app, user authentication might not be just one screen. Like many other apps, we could create two separate screens: one is for sign up (SignUpFragment) and the other one is for sign in (SignInFragment). It’s very common that sign-up screen has a button to load sign-in screen and vice versa. It’s possible that a user opens sign-in screen from sign-up, then taps the button that loads sign-up screen again. If we don’t do anything special, normally we would just load a new SignUpFragment instance into backstack. The backstack would then look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*NlOXHGzIsm0hOfGoXtB3Vw.jpeg" /></figure><p>Imagine if some user keeps tapping the button on each screen. It will keep loading new Fragment instances and create a deeply nested view navigation. What we really want here is to not load a new SignUpFragment instance, and instead go back to the previous instance. Ideally this should be handled smartly and automatically. To achieve this, we added a simple annotation targeting the Fragment class:</p><pre>@SingleInstanceFragment<br>public class SignUpFragment {}</pre><p>When loading a Fragment instance, first we check for the annotation, and if class is annotated with “SingleInstanceFragment”, we will pop to the previous Fragment instance of the existing singleton instance, and then load the new instance from there (this way we can also handle Fragment content update):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0279b1e1516c75b629d893360968f646/href">https://medium.com/media/0279b1e1516c75b629d893360968f646/href</a></iframe><p>Now developers just need to add the correct annotation to a fragment class definition and all of the backstack related operations for “singleton” fragments will be automatically taken care of.</p><h4>Presenting FragmentOperator</h4><p>We’ve encapsulated all of the above fragment management code in an open-source library called FragmentOperator.</p><p><a href="https://github.com/Tubitv/FragmentOperator">Tubitv/FragmentOperator</a></p><p>We want this library to help ease the pain of dealing with the Fragment backstack for fellow Android developers. We hope you’ll give it a shot and would love to get feedback on it.</p><h4>Pssst</h4><p>If you’re an Android developer who likes solving interesting and challenging engineering problems, we are <a href="https://tubitv.com/static/careers">hiring</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55a0ec3a517e" width="1" height="1" alt=""><hr><p><a href="https://code.tubitv.com/manage-android-fragment-backstack-through-tags-55a0ec3a517e">Managing Android Fragment Backstack Through Tags</a> was originally published in <a href="https://code.tubitv.com/">Tubi Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>