A Fragment example, build with ReKotlin and ReKotlin-Router
- Clone this repository
- Open in android studio.
- Build & Run.
You can dispatch the actions that invoke the Fragments
similar to Activity
.
val routes = arrayListOf(mainActivityRoute, backStackActivityRoute, oneFragmentRoute)
val action = SetRouteAction(route = routes)
val actionData = SetRouteSpecificData(route= routes,data = FragmentDataValue(activity, true))
mainStore.dispatch(actionData)
mainStore.dispatch(action)
The function SetRouteSpecificData
passes the data to Router to create the fragment.
The signature of FragmentDataValue
looks like
class FragmentDataValue(val activity: WeakReference<AppCompatActivity>, val addToBackStack: Boolean)
We need to pass the WeakReference
of the activity.
You should override the functions to create the Fragments
override fun pushRouteSegment(routeElementIdentifier: RouteElementIdentifier,
animated: Boolean,
completionHandler: RoutingCompletionHandler): Routable {
when (routeElementIdentifier) {
oneFragmentRoute -> {
return RoutableHelper.backStackFragmentRoutable(fragment = OneFragment(),
tag = "OneFragment")
}
twoFragmentRoute -> {
return RoutableHelper.backStackFragmentRoutable(fragment = TwoFragment(),
tag = "TwoFragment")
}
}
}
Let's look at the function backStackFragmentRoutable
fun backStackFragmentRoutable(fragment: Fragment,tag: String): FragmentRoutable {
val currentRoute = mainStore.state.navigationState.route
val intentData: FragmentDataValue =
mainStore.state.navigationState
.getRouteSpecificState<FragmentDataValue>(currentRoute)!!
val activity = intentData.activity.get()!!
val addToBackStack = intentData.addToBackStack
addFragment(fragment, activity, addToBackStack, tag,R.id.container_frame_back_stack)
//changeFragment(fragment,activity,addToBackStack,tag,R.id.container_frame_back)
return FragmentRoutable(activity.applicationContext)
}
It does the following
- get the current route from
navigationState
- From the current route, get the fragment's
intentData
- With the
intentData
, get the activity from itsWeakReference
and Boolean value whether to add to stack. - Call the function
addFragment
, helper method to create the fragment.
- TBD