-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFragmentActivity.kt
76 lines (61 loc) · 2.29 KB
/
FragmentActivity.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.example.myapplication
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
class FragmentActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fragment)
Log.d("life_cycle", "onCreate")
val fragmentOne : FragmentOne = FragmentOne()
//프라그먼트에 data를 넣어주는 방법
val bundle : Bundle = Bundle()
bundle.putString("hello", "hello")
fragmentOne.arguments = bundle
val button : Button = findViewById(R.id.button)
button.setOnClickListener{
//프라그먼트를 동적으로 작동하는 방법
//프라그먼트 붙이는 방법 replace/add
val fragmentManager : FragmentManager = supportFragmentManager
// Transaction
// 작업의 단위 -> 시작과 끝이 있다.
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, fragmentOne)
fragmentTransaction.commit()
// 끝을 내는 방법
// commit -> 시간 될 때 해 (좀 더 안정적)
// commitnow -> 지금 당장해
// commitAllowingStateLoss
}
val button2: Button = findViewById(R.id.button2)
button2.setOnClickListener {
// 프라그먼트 remove/detach 하는 방법
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.remove(fragmentOne)
fragmentTransaction.commit()
}
}
override fun onStart() {
super.onStart()
Log.d("life_cycle", "onStart")
}
override fun onPostResume() {
super.onPostResume()
Log.d("life_cycle", "onResume")
}
override fun onPause() {
super.onPause()
Log.d("life_cycle", "onPause")
}
override fun onStop() {
super.onStop()
Log.d("life_cycle", "onStop")
}
override fun onDestroy() {
super.onDestroy()
Log.d("life_cycle", "onDestroy")
}
}