Kotlinを使ってJavaと同じようにActivity間を遷移させる方法です。また同じActivity内で画面を変える方法もあります。
 
![[Android & Kotlin] Activityの遷移をさせる 1x1.trans - [Android & Kotlin] Activityの遷移をさせる](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
2024.1.1
Activityの遷移
Activityから別のActivityへの遷移ですが、結果的にActivityの画面遷移となります。ただ最近は画面の遷移だけであればFragmentを使うのが主流です。
 
Activityを画面の変更だけに使うにはコスト高です。Activityは画面管理以外にも色々とやることがあるからです。画面表示をActivityを変えずにFragmentで複数張り付けることが可能です。
 
以下Javaによる遷移をKotlinに置き換えています。
https://akira-watson.com/android/activity-1.html
Intent()によるActivityの画面遷移
「Activityを変えて画面遷移」をしたい場合には、Intent を使います。
IntentはActivityやServiceなどのコンポーネントを外部から起動するための仕組みです。
 
MainActivity 画面で 「MOVE」 ボタンをタップすると SubActivity に遷移、そこで [RETURN] ボタンのタップでMainActivity に戻る基本的なアプリを作ってみたいと思います。
 
MainActivity から SubActivity への遷移は
| 
					 1 2  | 
						val intent = Intent(applicationContext, SubActivity::class.java) startActivity(intent)  | 
					
Intent のインスタンスを、現在のActivityから、遷移先のActivityを引数に生成します
SubActivity::class.javaとなっていますが、ファイルはKotlinのSubActivity.ktです。
 
SubActivity からMainActivityへの戻りは
Activity を終了させます
| 
					 1  | 
						finish()  | 
					
Manifestに SubActivity を追加するのを忘れないように
| 
					 1 2 3  | 
						<activity     android:name=".SubActivity" > </activity>  | 
					
サンプルコード
 でまとめてみると
 
MainActivity.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  | 
						//package com.example.kotlinactivitytoactivity import android.content.Intent import android.os.Bundle import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import com.example.kotlinactivitytoactivity.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() {     private lateinit var binding: ActivityMainBinding     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         enableEdgeToEdge()         binding = ActivityMainBinding.inflate(layoutInflater)         setContentView(binding.root)         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->             val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)             insets         }         binding.sendButton.setOnClickListener {             val intent = Intent(application, SubActivity::class.java)             startActivity(intent)         }     } }  | 
					
 
activity_main.xml
| 
					 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  | 
						<?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"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/main"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">     <TextView         android:text="@string/main_activity"         android:textSize="30sp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.3" />     <Button         android:id="@+id/sendButton"         android:text="@string/move"         android:textSize="30sp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.5" /> </androidx.constraintlayout.widget.ConstraintLayout>  | 
					
SubActivity用のXMLレイアウトファイルが activity_sub.xml としたのでViewBindingの設定も変わります。
SubActivity.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  | 
						//package com.example.kotlinactivitytoactivity import android.os.Bundle import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat // ActivitySubBinding import com.example.kotlinactivitytoactivity.databinding.ActivitySubBinding class SubActivity : AppCompatActivity() {     // ActivitySubBinding     private lateinit var binding: ActivitySubBinding     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         enableEdgeToEdge()         // ActivitySubBinding         binding = ActivitySubBinding.inflate(layoutInflater)         setContentView(binding.root)         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.sub)) { v, insets ->             val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)             insets         }         binding.returnButton.setOnClickListener {             finish()         }     } }  | 
					
activity_sub.xml
| 
					 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  | 
						<?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"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/sub"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">     <TextView         android:text="@string/sub_activity"         android:textSize="30sp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.3" />     <Button         android:id="@+id/returnButton"         android:text="@string/move"         android:textSize="30sp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.5" /> </androidx.constraintlayout.widget.ConstraintLayout>  | 
					
リソースファイルです。retrunというキーワードは予約語なので使えないですね
strings.xml
| 
					 1 2 3 4 5 6 7  | 
						<resources>     <string name="app_name">KotlinActivityToActivity</string>     <string name="main_activity">Main Activity</string>     <string name="move">MOVE</string>     <string name="sub_activity">Sub Activity</string>     <string name="return_sub">RETURN</string> </resources>  | 
					
マニフェストです
AndroidManifest.xml
| 
					 1 2 3 4 5 6 7 8 9 10  | 
						<?xml version="1.0" encoding="utf-8"?>     ...     <application         ...         </activity>         <activity             android:name=".SubActivity" >         </activity>     </application> </manifest>  | 
					
 
ViewBindingのための設定
build.gradle
| 
					 1 2 3 4 5 6 7 8  | 
						... android {     ...     buildFeatures {         viewBinding = true     } } ...  | 
					
戻りは、ユーザーのリターンキーをタップしても戻れます。
Activityの遷移に伴ってデータの受け渡しは以下のようにします
 
References:
アクティビティ | Android Developers
一般的なインテント | Android Developers