안드로이드 앱을 개발하다 보면 로그아웃 처리나 '앱 완전 종료' 기능을 구현할 때 열려 있는 모든 액티비티를 한 번에 닫아야 하는 경우가 있습니다. 이 글에서는 Process.killProcess()를 활용해 앱의 모든 액티비티를 한꺼번에 종료하는 방법을 단계별로 알아보겠습니다.
동작 원리
핵심 아이디어는 간단합니다. 마지막 액티비티가 소멸(onDestroy)되는 시점에 앱 프로세스 자체를 강제로 종료하는 것입니다. 그러면 백 스택에 쌓여 있던 이전 액티비티까지 모두 함께 사라지며, 앱이 완전히 종료됩니다.
단계별 구현 방법
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="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/button"
android:text="Activity_One"
android:gravity="center"
android:layout_marginBottom="20sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click here to start Second Activity!"
android:layout_centerInParent="true" />
</RelativeLayout>
3단계 — MainActivity.java 작성
src/MainActivity.java에 아래 코드를 추가합니다. 버튼을 클릭하면 두 번째 액티비티가 시작됩니다.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
4단계 — SecondActivity 레이아웃 작성
새 액티비티(SecondActivity)를 생성한 뒤 res/layout/activity_second.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=".SecondActivity">
<Button
android:id="@+id/terminateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Terminate all the activities"
android:layout_centerInParent="true" />
</RelativeLayout>
5단계 — SecondActivity.java 작성
src/SecondActivity.java에 아래 코드를 추가합니다. 핵심은 onDestroy()에서 Process.killProcess(Process.myPid())를 호출해 프로세스를 종료하는 부분입니다.
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button button = (Button) findViewById(R.id.terminateButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
protected void onDestroy() {
Process.killProcess(Process.myPid());
super.onDestroy();
}
}
6단계 — AndroidManifest.xml 설정
androidManifest.xml에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
<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=".SecondActivity"></activity>
<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" 버튼을 클릭하면 현재 액티비티가 종료되면서 onDestroy()가 호출되고, 이어서 프로세스 전체가 종료되어 모든 액티비티가 한 번에 닫히는 것을 확인할 수 있습니다.

추가 팁: finishAffinity() 활용
API 16(젤리빈) 이상에서는 프로세스를 강제로 종료하는 대신 finishAffinity() 메서드를 사용할 수도 있습니다. 이 메서드는 같은 태스크에 속한 액티비티들을 모두 정상적인 생명주기 흐름으로 종료해 주므로 더 안전한 방식으로 권장됩니다. 반면 위 예제처럼 프로세스 자체를 종료하면 앱 상태가 완전히 초기화되므로, 로그아웃처럼 깔끔한 종료가 필요한 상황에 유용합니다.
또한 최신 프로젝트에서는 android.support.v7.app.AppCompatActivity 대신 AndroidX의 androidx.appcompat.app.AppCompatActivity를 사용한다는 점도 참고하세요.