daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Mar 24th, 2019 61 in 2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. abstract class BaseFragment : Fragment() {
  2.  
  3.     protected var restoredFromBackstack = false
  4.         private set
  5.  
  6.     private var backstackMessage: Bundle? = null
  7.  
  8.     /**
  9.      * Allows to pass data to the fragment previous on the backstack
  10.      * when this fragment finishes. Requires having targetFragment set during transaction.
  11.      */
  12.     protected open fun composeBackstackMessage(): Bundle? = null
  13.  
  14.     /**
  15.      * Process data from the finishing fragment.
  16.      */
  17.     protected open fun onBackstackMessageReceived(bundle: Bundle) {}
  18.  
  19.     /**
  20.      * Called when fragment is restored from backstack.
  21.      */
  22.     @CallSuper
  23.     protected open fun onRestoreFromBackstack() {
  24.         backstackMessage?.let { onBackstackMessageReceived(it) }
  25.         backstackMessage = null
  26.     }
  27.  
  28.     @CallSuper
  29.     override fun onCreate(savedInstanceState: Bundle?) {
  30.         super.onCreate(savedInstanceState)
  31.         restoredFromBackstack = false
  32.     }
  33.  
  34.  
  35.     override fun onStart() {
  36.         super.onStart()
  37.  
  38.         if (restoredFromBackstack) {
  39.             onRestoreFromBackstack()
  40.         }
  41.     }
  42.  
  43.     override fun onDestroyView() {
  44.         restoredFromBackstack = true
  45.         super.onDestroyView()
  46.     }
  47.  
  48.     override fun onDestroy() {
  49.         if (activity?.isChangingConfigurations != true) {
  50.             (targetFragment as? BaseFragment)?.let { target ->
  51.                 composeBackstackMessage()?.let { backstackMessage ->
  52.                     if (target.lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
  53.                         target.onBackstackMessageReceived(backstackMessage)
  54.                     } else {
  55.                         target.backstackMessage = backstackMessage
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.  
  61.         super.onDestroy()
  62.     }
  63.  
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top