You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用被系统杀死后如何始终启动LauncherActivity?附8.1.0权限问题

Great question! This is such a common headache on Android—when the system kills your app process (like when revoking permissions) and then tries to revive the last activity in your task stack, leading to crashes because all your app's initialized state is gone. Let's walk through how to fix this so your app always boots back to LauncherActivity after a process kill.

Why This Happens

Here's the breakdown of what's going on in your test flow:

  • You build up a task stack: LauncherActivity → Activity A → Activity B
  • When you revoke a permission, Android immediately kills your app process to enforce the new permission state.
  • When you reopen via recent tasks, Android tries to restore the top of the task stack (Activity B) first. But since the entire process was wiped, any global variables, initialized libraries, or cached data Activity B depends on are gone—hence the crash.

Solution: Detect Process Restarts & Redirect to Launcher

We need a way to tell when the app has been restarted from a killed state, then bounce any non-launcher activity back to LauncherActivity before it can crash. Here's how to implement it:

1. Track Process Restarts with a Session ID

First, create a custom Application class to track if this is a fresh process launch or a restart. We'll use a unique session ID stored in SharedPreferences:

import android.app.Application;
import android.content.SharedPreferences;
import java.util.UUID;

public class MyApp extends Application {
    public static boolean isProcessRestarted = false;
    private static final String PREFS_NAME = "app_session";
    private static final String KEY_LAST_SESSION = "last_session_id";

    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        String lastSessionId = prefs.getString(KEY_LAST_SESSION, null);
        
        // Generate a new unique ID for this process run
        String currentSessionId = UUID.randomUUID().toString();
        prefs.edit().putString(KEY_LAST_SESSION, currentSessionId).apply();
        
        // If there was a previous session ID, this means the process was restarted
        isProcessRestarted = lastSessionId != null;
    }
}

2. Register the Custom Application

Update your AndroidManifest.xml to use this class (so it runs every time your process starts):

<application
    android:name=".MyApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <!-- Your activities go here -->
</application>

3. Add the Redirect Check to All Non-Launcher Activities

In every activity that isn't your LauncherActivity, add this check right at the start of onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // If the process was restarted and this isn't the launcher, redirect
    if (MyApp.isProcessRestarted && !(this instanceof LauncherActivity)) {
        Intent launcherIntent = new Intent(this, LauncherActivity.class);
        // Clear the entire old task stack and start fresh
        launcherIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(launcherIntent);
        finish(); // Close this activity immediately so it doesn't load
        return;
    }
    
    // Your normal activity setup code goes here
    setContentView(R.layout.activity_b);
    // ... rest of your logic
}

4. Optional: Clean Up LauncherActivity (If Needed)

If you want to make extra sure LauncherActivity starts fresh, you can add a small check there too:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    if (MyApp.isProcessRestarted) {
        // Clear any leftover stack state just in case
        getIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    
    setContentView(R.layout.activity_launcher);
    // ... rest of your launcher code
}

Why This Works

  • The session ID trick reliably detects process restarts: if there's a previous session ID saved, it means the app was run before this process launch (i.e., the process was killed and restarted).
  • The FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK flags wipe out the old task stack entirely, so your app starts fresh with LauncherActivity—no more crashed activities from incomplete state.

内容的提问来源于stack exchange,提问作者Yang Zhou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:40:09