이 튜토리얼에서는 Kotlin을 사용해 안드로이드에서 실행 중인 모든 액티비티를 한 번에 종료하는 방법을 단계별로 알아봅니다. 두 개의 액티비티를 만들고, 두 번째 액티비티에서 버튼 하나만 누르면 앱 전체가 완전히 종료되도록 구현해 보겠습니다.
프로젝트 생성하기
1단계 − Android Studio에서 새 프로젝트를 생성합니다. 상단 메뉴에서 File → New Project를 선택한 뒤, 필요한 정보를 모두 입력해 새 프로젝트를 만들어 주세요.
첫 번째 액티비티 레이아웃 작성
2단계 − 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>MainActivity에 버튼 이벤트 연결
3단계 − src/MainActivity.kt 파일에 아래 코드를 작성합니다. 버튼을 클릭하면 Intent를 통해 두 번째 액티비티가 실행됩니다.
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)
}
}
}두 번째 액티비티 만들기
4단계 − 새로운 액티비티를 생성하고 아래와 같이 코드를 작성합니다.
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 −
여기서 핵심 포인트는 onDestroy() 콜백 안에서 exitProcess(0)를 호출하는 것입니다. 버튼 클릭 시 finish()로 현재 액티비티가 종료되면, 곧바로 프로세스 자체가 종료되어 백 스택에 남아 있던 첫 번째 액티비티까지 함께 닫히게 됩니다.
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)
}
}매니페스트 설정 확인
5단계 − AndroidManifest.xml에 아래 코드를 추가합니다. MainActivity가 런처(LAUNCHER) 액티비티로 등록되어 있어야 앱이 정상적으로 시작됩니다.
<?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 Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run(실행) 아이콘
을 클릭하세요. 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 앱의 기본 화면이 표시됩니다.
버튼을 눌러 두 번째 액티비티로 이동한 후, "Terminate all the activities" 버튼을 클릭하면 열려 있던 모든 액티비티가 종료되면서 앱이 완전히 닫히는 것을 확인할 수 있습니다.


