-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathActivityExt.kt
62 lines (56 loc) · 1.66 KB
/
ActivityExt.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* Extentions activity level */
import android.annotation.SuppressLint
import android.os.Build
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.support.v7.app.AppCompatActivity
import android.view.View
/**
* Using it for replacement fragment
*
* @param fragment => your fragment class
* @param frameId => your container layout id
*/
fun AppCompatActivity.replaceFragmentInActivity(
fragment: Fragment,
frameId: Int
) {
supportFragmentManager.transact {
replace(frameId, fragment)
}
//=========== How to using it ===========
// override fun onYourMethod() {
// replaceFragmentInActivity(Fragment.newInstance(), R.id.frame_container)
// }
//=======================================
}
/**
* Using it for replacement fragment and add it into backstack
*
* @param fragment => your fragment class
* @param frameId => your container layout id
* @param TAG => your TAG usually the name of activity class
*/
fun AppCompatActivity.replaceFragmentInActivityWithBackStack(
fragment: Fragment,
frameId: Int,
TAG: String?
) {
supportFragmentManager.transact {
replace(frameId, fragment)
addToBackStack(TAG)
}
//=========== How to using it ===========
// override fun onYourMethod() {
// replaceFragmentInActivityWithBackStack(Fragment.newInstance(), R.id.frame_container, TAG)
// }
//=======================================
}
private inline fun FragmentManager.transact(
action: FragmentTransaction.() -> Unit
) {
beginTransaction().apply {
action()
}.commit()
}