import androidx.annotation.IdRes import androidx.fragment.app.* /** * Push [Fragment] into container that gets added to back-stack and later be popped by popping the * back-stack. * * Sample Usage: * ``` * childFragmentManager.push(myFragment, R.id.childContainer) * ``` * * Here's what it does in: * 1. Creates a fragment transaction. * 1. Sets slide animations. * - Fragment enters from right edge and exits to the right edge. * 1. Adds fragment to the container with back-stack. * 1. The tag of back-stack gets determined by the [`singleName`](Class.getSimpleName) property * of the class. * 1. The transaction gets committed based on `now` and `allowStateLoss` parameters. See * [FragmentManager.transaction] extension from `androidx` to learn more about how these * parameters affect the transaction. * * @see FragmentManager.transaction */ fun <T : Fragment> FragmentManager.push( fragment: T, @IdRes containerViewId: Int, now: Boolean = false, allowStateLoss: Boolean = false ) { transaction(now, allowStateLoss) { setSlideAnimations() addWithBackStack(containerViewId, fragment) } } fun <T : Fragment> FragmentTransaction.addWithBackStack( @IdRes containerViewId: Int, fragment: T ) { add(containerViewId, fragment, fragment.javaClass.simpleName) addToBackStack(fragment.javaClass.simpleName) } fun FragmentTransaction.setSlideAnimations() { setCustomAnimations( R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right ) }