프로그래밍/안드로이드, 코틀린
[안드로이드 코틀린] 부모 액티비티에서 자식 프래그먼트 함수 호출하기
toonraon
2023. 2. 20. 23:34
두괄식으로 결론부터 조지자면
// 액티비티에 이 코드 삽입
val fragment = supportFragmentManager.findFragmentById(R.id.fragment_container) as MyFragment
fragment.myFragmentFuncion()
이렇게 하시면 됩니다.
메인 액티비티 소스 코드(MainActivity.kt)
package com.example.test
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, MyFragment.newInstance())
.commit()
}
override fun onStart() {
super.onStart()
val fragment = supportFragmentManager.findFragmentById(R.id.fragment_container) as MyFragment
fragment.myFragmentFunction()
}
}
참고로 onStart에 있는 코드를 onCreate에 쓰면 val fragment가 null이 되면서 앱이 종료됩니다.
왜냐하면 onCreate가 끝나고 나서야 프래그먼트가 생성 완료되기 때문입니다.
액티비티와 프래그먼트 간의 생명 주기
액티비티의 onCreate 시작 -> supportFragmentManager 부분 진입 -> 프래그먼트의 onAttach 시작&완료 -> 프래그먼트의 onCreate 시작&완료 -> 액티비티의 onCreate 완료 -> 프래그먼트의 onCreateView 시작&완료 -> 프래그먼트의 onViewCreated 시작&완료 -> 액티비티의 onStart
따라서 액티비티의 onCreate 안에서는 아직 프래그먼트가 생성이 덜 되었고, 액티비티의 onStart가 호출될 때서야 프래그먼트가 완전히 다 생성된 상태입니다.
메인 액티비티의 레이아웃(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
그냥 아무것도 없고 가운데에 fragment_container라는 id의 레이아웃만 있습니다.
findFragmentById에서 id가 바로 이 컨테이너의 id입니다.
프래그먼트의 레이아웃(MyFragment.kt)
package com.example.test
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
class MyFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.my_fragment, container, false)
}
fun myFragmentFunction() {
Toast.makeText(requireContext(), "프래그먼트에서 호출된 함수입니다.", Toast.LENGTH_SHORT).show()
}
companion object {
@JvmStatic
fun newInstance() = MyFragment()
}
}
별 것 없고 호출되면 Toast를 띄워주는 함수가 하나 있습니다.
프래그먼트 레이아웃(my_fragment.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
가운데에 텍스트뷰 하나만 달랑 있는 레이아웃입니다.
실행 결과
참고로
findFragmentById 말고 사용할 수 있는 방법으로는
findFragmentByTag가 있습니다. supportFragmentManager에서 .add(R.id.fragment_container, MyFragment.newInstance(), "태그")로 태그를 지정해준 뒤 findFragmentByTag("태그") 이렇게 하면 됩니다.
아니면 진짜 무식한 방법으로는 supportFragmentManager.fragments[0]을 하는 방법도 있습니다. (fragment가 1개뿐일 경우에)