Android Kotlin ActivityからFragmentを追加

個人開発したアプリの宣伝
目的地が設定できる手帳のような使い心地のTODOアプリを公開しています。
Todo with Location

Todo with Location

  • Yoshiko Ichikawa
  • Productivity
  • Free

スポンサードリンク

supportFragmentManagerでlayoutにfragmentを割り当てる

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val fragment = MyFragment()
        val t = supportFragmentManager.beginTransaction()
        t.add(R.id.layout_id, fragment)
        t.commit()
    }
}