Last active
April 30, 2019 19:15
-
-
Save raxityo/0eda677728a3503d279d9e6160362904 to your computer and use it in GitHub Desktop.
FragmentManager extensions. A set of extensions to easily push a fragment to a container with one function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<translate | |
android:duration="@android:integer/config_mediumAnimTime" | |
android:fromXDelta="100%p" | |
android:toXDelta="0" /> | |
</set> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<translate | |
android:duration="@android:integer/config_mediumAnimTime" | |
android:fromXDelta="0" | |
android:toXDelta="-100%p" /> | |
</set> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment