0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

kotlin androidアプリについてのメモ~Kotlin編~

Last updated at Posted at 2023-06-27

Android StudioのTemplate projectのコードを読んで、
「これだけ理解すればあとは頑張って(?)読める」ものをまとめました。

次のようなクラスがあるとします

MainActivity.kt
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.appBarMain.toolbar)

        binding.appBarMain.fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }
        val drawerLayout: DrawerLayout = binding.drawerLayout
        val navView: NavigationView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(setOf(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

AppCompatActivity:

Android Support Libraryの一部であり、古いバージョンのAndroidプラットフォームでも最新の機能を利用できるようになるクラス

AppBarConfiguration:

アクションバー(Toolbar)の設定やナビゲーションの設定を保持するクラス

ActivityMainBinding:

View Bindingの一種であり、レイアウトファイルとアクティビティのバインディングを行うクラス
ビューの参照やイベントのリスナーの設定などを簡単に行うことができる

onCreate:

Androidアクティビティのライフサイクルメソッドの一つ
アクティビティが初めて作成されるときに呼び出される

savedInstanceState:

アクティビティが以前に破棄された場合に、復元に使用される情報を保持するためのBundleオブジェクト

Bundle:

キーと値のペアを格納するオブジェクト
putメソッドを使用して追加される
Intentに追加されることで、別のアクティビティにデータを渡すために使用される
また、フラグメントへの引数としても使用することができる

R.id.~:

xmlの、android:id="@+id/~を参照

binding = ActivityMainBinding.inflate(layoutInflater)→setContentView(binding.root):

inflate()は、レイアウトファイル(activity_main.xml)を展開して、その内容に基づいたActivityMainBindingオブジェクトを生成
layoutInflaterによってXMLファイルのリソースIDやファイルパスを受け取り、その内容をビューオブジェクトに変換して返す(理解が難しい)
setContentView(binding.root)は、アクティビティのcontentビューとして、ActivityMainBindingオブジェクトのrootビューを設定
つまり、activity_main.xmlファイルの内容がアクティビティの画面に表示される

findNavController():

現在のアクティビティやフラグメントのナビゲーションコントローラー(NavController)を取得
ナビゲーションコントローラーは、画面間の遷移などのナビゲーション操作を担当

AppBarConfiguration(setOf ...):

クラスを使用して、アクションバーの構成情報を作成
これにより、アクションバーのタイトルやアップボタンの動作を制御することができる

setupActionBarWithNavController():

アクションバーをナビゲーションコントローラーと関連付ける
これにより、タイトルやアップボタンの表示やクリックイベントの処理が自動的に設定

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?