Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách ngăn chặn quay lại Activity trước trong Android

Giới thiệu

Có nhiều tình huống không cho phép người dùng quay lại Activity trước đó (ví dụ: sau khi đăng nhập, màn hình thanh toán...). Ví dụ này minh họa 2 cách: ghi đè onBackPressed() và thiết lập android:noHistory="true" trong Manifest.

Bước 1: Tạo dự án mới

Android Studio → File → New Project.

Bước 2: Giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center"
    android:orientation="vertical">

    <EditText
        android:id="@+id/enterNumber"
        android:layout_width="match_parent"
        android:hint="Nhập số điện thoại"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:textSize="18sp"
        android:textAlignment="center"
        android:text="Nhấn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Bước 3: Ghi đè onBackPressed (src/MainActivity.java)

package com.example.andy.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    int view = R.layout.activity_main;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(view);
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed(); // Không gọi super để chặn hành động mặc định
        Toast.makeText(MainActivity.this, "Không có hành động quay lại", Toast.LENGTH_LONG).show();
        return;
    }
}

Bước 4: Cấu hình Manifest (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.andy.myapplication">

    <uses-permission android:name="android.permission.VIBRATE" />

    <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"
            android:noHistory="true"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Giải thích 2 phương pháp

1. Ghi đè onBackPressed()

  • Không gọi super.onBackPressed() → Chặn hoàn toàn nút Back
  • Có thể hiển thị Toast, Dialog xác nhận, hoặc logic tùy chỉnh

2. android:noHistory="true" trong Manifest

  • Activity không được lưu trong back stack
  • Khi rời Activity (startActivity khác, nhấn Home...), Activity bị destroy ngay
  • Người dùng không thể quay lại được Activity này bằng nút Back

Chạy ứng dụng

Chạy app → Màn hình hiển thị EditText và TextView. Nhấn nút Back vật lý/ảo → Toast hiển thị "Không có hành động quay lại", Activity không bị đóng.