【Kotlin入門】Androidでアクティビティを切り替える方法をわかりやすく解説
Kotlinを使えば、Androidアプリ内の複数のアクティビティ(Activity)を簡単に切り替えることができます。この記事では、Intentを使った画面遷移の基本実装を、サンプルコード付きで段階的に解説します。初心者の方でもそのままコピー&ペーストで動かせる内容です。
ステップ1:新規プロジェクトを作成する
まずはAndroid Studioで新しいプロジェクトを作成しましょう。メニューから「File」→「New Project」を選択し、必要な項目を入力してプロジェクトを作成します。
ステップ2:activity_main.xml にレイアウトを定義する
res/layout/activity_main.xml に以下のコードを追加します。中央に配置したボタンをタップすると、セカンドアクティビティへ遷移するシンプルなレイアウトです。
コード例
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:textColor="@android:color/holo_purple" android:text="MainActivity" android:textSize="24sp" android:textStyle="bold" /> <Button android:id="@+id/btnOpenAct2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Open Second Activity" /> </RelativeLayout>
ステップ3:MainActivity.kt を実装する
src/MainActivity.kt に以下のコードを追加します。Intentの第二引数に遷移先のクラスを指定し、startActivity()を呼び出すことで画面遷移が実現できます。
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val button: Button = findViewById(R.id.btnOpenAct2)
button.setOnClickListener {
val intent = Intent(this@MainActivity, NewActivity::class.java)
startActivity(intent)
}
}
}
ステップ4:新しい空のアクティビティを作成する
次に、遷移先となる空のアクティビティ(NewActivity)を新規作成し、以下のコードを追加します。こちらも同様にIntentを使ってメインアクティビティへ戻れるようにしています。
NewActivity.kt
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class NewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new)
title = "KotlinApp"
val button: Button = findViewById(R.id.btnOpenMain)
button.setOnClickListener {
val i = Intent(this@NewActivity, MainActivity::class.java)
startActivity(i)
}
}
}
activity_new.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="New Activity" android:textColor="@android:color/holo_green_dark" android:textSize="24sp" android:textStyle="bold" /> <Button android:id="@+id/btnOpenMain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Go back to MainActivity" /> </RelativeLayout>
ステップ5:AndroidManifest.xml を確認する
androidManifest.xml に以下のコードを追加します。すべてのアクティビティはマニフェストに宣言する必要がある点に注意してください。Android Studioから「New Activity」として作成した場合は、通常自動的に宣言されます。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
アプリを実行してみよう
それでは、実際にアプリケーションを実行してみましょう。ここでは実機のAndroidデバイスがパソコンに接続されているものとして説明します。Android Studioからアプリを実行するには、プロジェクト内のいずれかのアクティビティファイルを開き、ツールバーにある実行アイコン
をクリックします。候補から接続済みのモバイルデバイスを選択すると、端末にアプリの初期画面が表示されます。


-
Androidでアクティビティ間にドローアブルを渡す方法【サンプルコード付きで解説】
この記事では、Androidアプリにおいて2つのアクティビティ間でドローアブル(画像リソース)を受け渡す方法を、実際に動作するサンプルコードとともに段階的に解説します。実装のポイント:ドローアブル本体ではなく「リソースID」を渡すドローアブルオブジェクトをIntentに直接渡そうとすると、データサイズが大きすぎてエラーになる場合があります。そこで本記事では、ドローアブルのリソースID(int値)を putExtra() で渡し、受け取り側で setImageResource() を使って表示するというシンプルかつ確実な手法を採用します。これが画像リソースの受け渡しにおけるベストプラクティスです
-
Androidで複数のアクティビティ(画面)を切り替える方法【サンプルコード付き】
本記事では、Androidアプリで複数の画面(アクティビティ)をIntentを使って切り替える方法を、サンプルコードとともにわかりやすく解説します。ボタンをタップすると「MainActivity」から「SecondActivity」へ遷移し、そこから再び元の画面へ戻れるというシンプルな例です。 手順1:Android Studioで新規プロジェクトを作成する Android Studioを開き、メニューの「File」→「New Project」を選択して、必要な項目を入力のうえ新しいプロジェクトを作成します。 手順2:activity_main.xmlにコードを追加する メイン画面のレイアウト