Skip to content

A Fragment example, build with ReKotlin and ReKotlin-Router

Notifications You must be signed in to change notification settings

kmmraj/reKotlinFragmentExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ReKotlin Fragment Example

A Fragment example, build with ReKotlin and ReKotlin-Router

Installation

  1. Clone this repository
  2. Open in android studio.
  3. Build & Run.

Details

Dispatching actions that request routing Fragments

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.

What kind data need to be passed?

The signature of FragmentDataValue looks like

class FragmentDataValue(val activity: WeakReference<AppCompatActivity>, val addToBackStack: Boolean)

We need to pass the WeakReference of the activity.

Routable that creates Fragments

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 its WeakReference and Boolean value whether to add to stack.
  • Call the function addFragment, helper method to create the fragment.

Unit Testing

  1. TBD

Releases

No releases published

Packages

No packages published

Languages