Android
 Computer >> コンピューター >  >> プログラミング >> Android

KotlinでAndroidアプリの全アクティビティを一度に終了する方法

はじめに

このチュートリアルでは、Kotlinを使用してAndroidアプリのすべてのアクティビティを一度に終了(閉じる)する方法を、サンプルコードとともに段階的に解説します。複数の画面をまたいでアプリ全体を終了させたい場合に役立つテクニックです。

手順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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/button"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="Activity One"
        android:textColor="@android:color/background_dark"
        android:textStyle="bold" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Start Activity two" />
</RelativeLayout>

このレイアウトには、2つ目のアクティビティを起動するためのボタンが配置されています。

手順3:MainActivity.kt の実装

src/MainActivity.kt に以下のコードを追加します。

package com.example.q28
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
    lateinit var button: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        button = findViewById(R.id.button)
        button.setOnClickListener {
            val intent = Intent(this@MainActivity, SecondActivity::class.java)
            startActivity(intent)
        }
    }
}

ボタンをタップすると、Intentを使ってSecondActivityへ遷移するシンプルな実装です。

手順4:2つ目のアクティビティの作成

新しいアクティビティを作成し、以下のコードを追加します。

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/terminateButton"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="Activity Two"
        android:textColor="@android:color/background_dark"
        android:textStyle="bold" />
    <Button
        android:id="@+id/terminateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Terminate all the activities" />
</RelativeLayout>

SecondActivity.kt

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import kotlin.system.exitProcess
class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        title = "KotlinApp"
        val button: Button = findViewById(R.id.terminateButton)
        button.setOnClickListener {
            finish()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        exitProcess(0)
    }
}

ここでのポイントは、ボタンをタップしたときに finish() を呼び出し、onDestroy() 内で exitProcess(0) を実行することです。これにより、バックスタックに残っているすべてのアクティビティを含め、アプリ全体を完全に終了させることができます。

手順5:AndroidManifest.xml の設定

androidManifest.xml に以下のコードを追加します。

<?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からアプリを起動するには、プロジェクト内の任意のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。表示された選択肢から接続したモバイルデバイスを選択すると、端末に初期画面が表示されます。

最初の画面(Activity One)のボタンをタップして2つ目の画面(Activity Two)に遷移し、「Terminate all the activities」ボタンを押すと、すべてのアクティビティが終了しアプリが完全に閉じられることが確認できます。

  1. 【Android】すべてのアクティビティを一括で終了する方法を徹底解説

    Androidアプリでは、画面遷移のたびにアクティビティがスタックに積み上げられていきます。本記事では、ボタンをタップするだけでアプリ内のすべてのアクティビティを一度に閉じ、アプリを完全に終了させる実装方法を、サンプルコード付きで段階的に解説します。仕組みのポイント今回のサンプルの鍵となるのは、SecondActivityのonDestroy()内で呼び出すProcess.killProcess(Process.myPid())です。このメソッドにより、アクティビティだけでなくアプリのプロセスそのものが強制終了され、バックスタックに残っている他のアクティビティもまとめて破棄されます。手順1:新

  2. KotlinでAndroidアプリの全アクティビティを一度に終了する方法

    はじめにこのチュートリアルでは、Kotlinを使用してAndroidアプリのすべてのアクティビティを一度に終了(閉じる)する方法を、サンプルコードとともに段階的に解説します。複数の画面をまたいでアプリ全体を終了させたい場合に役立つテクニックです。手順1:新規プロジェクトの作成まず、Android Studioで新しいプロジェクトを作成します。メニューから「File」→「New Project」を選択し、必要な項目をすべて入力してプロジェクトを作成してください。手順2:レイアウトファイル(activity_main.xml)の作成res/layout/activity_main.xml に以下の