Advertisement
Guest User

ChildFragment

a guest
Jan 22nd, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.18 KB | None | 0 0
  1. abstract class ChildFragment: Fragment() {
  2.  
  3.     interface Parent {
  4.         fun titleChanged(newTitle: String?)
  5.         fun changeChild(newChild: ChildFragment)
  6.         fun changeContent(newContent: Fragment, addToBackStack: Boolean)
  7.         val child: ChildFragment?
  8.     }
  9.  
  10.     protected val parent: Parent?
  11.         get() = if(parentFragment is Parent) {
  12.             parentFragment as Parent
  13.         } else null
  14.  
  15.  
  16.     var title: String = ""
  17.         set(value) {
  18.             field = value
  19.             parent?.titleChanged(value)
  20.         }
  21.  
  22.     override fun onCreate(savedInstanceState: Bundle?) {
  23.         super.onCreate(savedInstanceState)
  24.         savedInstanceState?.getBundle(CHILD_FRAGMENT_STATE)?.apply {
  25.             title = getString(TITLE_KEY)
  26.         }
  27.     }
  28.  
  29.     override fun onSaveInstanceState(outState: Bundle) {
  30.         super.onSaveInstanceState(outState)
  31.         outState.putBundle(CHILD_FRAGMENT_STATE, Bundle().apply {
  32.             putString(TITLE_KEY, title)
  33.         })
  34.     }
  35.  
  36.     private companion object {
  37.         private const val CHILD_FRAGMENT_STATE = "CHILD_FRAGMENT_STATE_KEY"
  38.         private const val TITLE_KEY = "CHILD_FRAGMENT_TITLE_KEY"
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement