Giới thiệu
Bài viết minh họa cách Activity.finish() hoạt động: kết thúc Activity hiện tại, xóa khỏi back stack, quay về Activity trước đó (hoặc Home nếu là Activity root).
Các bước thực hiện
Bước 1: Tạo dự án mới
Khởi tạo project Android Studio.
Bước 2: MainActivity với Button chuyển màn (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:text="Main Activity!"
android:layout_marginTop="25sp"
android:textSize="25dp"
android:padding="15dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Activity Tiếp Theo" />
</RelativeLayout>
Bước 3: MainActivity gọi finish() sau startActivity (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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, NewActivity.class);
startActivity(intent);
finish(); // Quan trọng: Kết thúc MainActivity ngay sau khi start NewActivity
});
}
}
Bước 4: Tạo NewActivity (File → New → Activity → Empty Activity)
Bước 5: Layout NewActivity (res/layout/activity_new.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Activity!"
android:textStyle="bold"
android:textColor="@color/colorAccent"
android:layout_marginTop="25sp"
android:textSize="25dp"
android:padding="15dp" />
</RelativeLayout>
Bước 6: NewActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
}
Bước 7: 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=".NewActivity"></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>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run.
- MainActivity hiển thị → Nhấn "Activity Tiếp Theo"
- Chuyển sang NewActivity (MainActivity đã finish, không còn trong back stack)
- Ở NewActivity, nhấn Back hệ thống → Không quay về MainActivity (vì đã finish) → Về Home/Launcher



Cơ chế finish() chi tiết
Điều gì xảy ra khi gọi finish()?
- Activity nhận signal kết thúc
- Gọi
onPause()→onStop()→onDestroy() - Activity bị xóa khỏi Back Stack của Task
- Nếu là Activity root của Task → Task kết thúc, về Home
- Nếu có Activity trước đó trong stack → Resume Activity đó
Khi nào dùng finish()?
- Login/Register thành công → finish LoginActivity, không cho back về login
- Splash screen → finish sau khi chuyển MainActivity
- Flow một chiều: Wizard, onboarding, payment success
- Đóng dialog/activity con sau khi xong việc
Các biến thể finish()
finish(); // Đóng activity hiện tại finishAffinity(); // Đóng activity này + tất cả activity cùng task (API 16+) finishAndRemoveTask(); // Đóng task hoàn toàn, xóa khỏi recent apps (API 21+) finishAfterTransition(); // Đóng sau khi transition animation xong (API 21+)
Truyền kết quả về Activity trước (setResult)
// Trong Activity con (NewActivity)
Intent resultIntent = new Intent();
resultIntent.putExtra("result_data", "Kết quả từ NewActivity");
setResult(RESULT_OK, resultIntent);
finish();
// Trong Activity cha (MainActivity) - override onActivityResult (cũ) hoặc dùng ActivityResultLauncher (mới)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
String result = data.getStringExtra("result_data");
}
}
Lưu ý
finish()không đồng bộ - Activity không biến mất ngay lập tức- Code sau
finish()vẫn chạy cho đến khi method return - Không gọi
finish()trongonCreate()trướcsetContentView()(crash) - Dùng
isFinishing()hoặcisDestroyed()để check state