ActivityExtensions.kt 900 Bytes
Newer Older
1 2 3 4
package chat.rocket.android.util

import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
5
import chat.rocket.android.R
6 7 8

fun AppCompatActivity.addFragment(tag: String, layoutId: Int, newInstance: () -> Fragment) {
    val fragment = supportFragmentManager.findFragmentByTag(tag) ?: newInstance()
9 10 11
    supportFragmentManager.beginTransaction()
            .replace(layoutId, fragment, tag)
            .commit()
12 13 14 15
}

fun AppCompatActivity.addFragmentBackStack(tag: String, layoutId: Int, newInstance: () -> Fragment) {
    val fragment = supportFragmentManager.findFragmentByTag(tag) ?: newInstance()
16 17 18 19 20
    supportFragmentManager.beginTransaction()
            .setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right)
            .replace(layoutId, fragment, tag)
            .addToBackStack(tag)
            .commit()
21
}