-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstFragment.kt
30 lines (29 loc) · 1.06 KB
/
FirstFragment.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
package com.example.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
class FirstFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val v = inflater.inflate(R.layout.fragment_first, container, false)
val context = activity as AppCompatActivity
val btnNavigate: Button = v.findViewById(R.id.btnNavigate)
btnNavigate.setOnClickListener {
context.replaceFragment(SecondFragment())
}
return v
}
fun AppCompatActivity.replaceFragment(fragment: Fragment){
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.host,fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}