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

Android新手求助:如何防止应用首次运行后打开部分Activity?

Hey there, fellow Android newbie! Let me break down exactly how you can prevent certain Activities from being accessed on your app's first launch—this is a super common use case, so you’re in the right place.

Solution: Use SharedPreferences to Track First-Run Status

The core idea is to store a persistent flag that tells us whether the app has completed its initial setup (like onboarding or first-time initialization). SharedPreferences is perfect for this—it’s Android’s built-in lightweight storage for simple key-value pairs.

Step 1: Create a Helper to Manage the First-Run Flag

First, let’s make a simple utility class (or you can handle this directly in your Activities) to read and update the first-run status:

Kotlin Example

object AppFirstRunManager {
    private const val PREF_FILE_NAME = "AppLaunchPrefs"
    private const val KEY_IS_FIRST_RUN = "is_first_launch"

    // Check if this is the app's first run
    fun isFirstRun(context: Context): Boolean {
        val prefs = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE)
        // Default to true if the key doesn't exist (first launch)
        return prefs.getBoolean(KEY_IS_FIRST_RUN, true)
    }

    // Mark the first run as completed once the user finishes setup
    fun markFirstRunCompleted(context: Context) {
        val prefs = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE)
        prefs.edit().putBoolean(KEY_IS_FIRST_RUN, false).apply()
    }
}

Java Example

public class AppFirstRunManager {
    private static final String PREF_FILE_NAME = "AppLaunchPrefs";
    private static final String KEY_IS_FIRST_RUN = "is_first_launch";

    public static boolean isFirstRun(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
        return prefs.getBoolean(KEY_IS_FIRST_RUN, true);
    }

    public static void markFirstRunCompleted(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
        prefs.edit().putBoolean(KEY_IS_FIRST_RUN, false).apply();
    }
}

Step 2: Mark First-Run as Completed After Initial Setup

Once the user finishes the mandatory first-time flow (like completing onboarding, logging in, or accepting terms), call the markFirstRunCompleted() method to update the flag. For example, in your onboarding screen’s "Enter App" button:

// Kotlin
btnEnterApp.setOnClickListener {
    AppFirstRunManager.markFirstRunCompleted(this)
    startActivity(Intent(this, MainActivity::class.java))
    finish() // Close the onboarding screen
}

Step 3: Block Access to Restricted Activities

Now, to prevent the restricted Activities from opening on first launch, add a check at the start of their onCreate() method. If it’s still the first run, redirect the user to your allowed screen (like onboarding or MainActivity) and close the restricted one:

// Inside your RestrictedActivity's onCreate()
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Check if we're still in first-run mode
    if (AppFirstRunManager.isFirstRun(this)) {
        // Redirect to onboarding or main screen
        startActivity(Intent(this, OnboardingActivity::class.java))
        finish() // Don't let the user stay on the restricted screen
        return // Skip the rest of the setup code
    }

    // Proceed with normal Activity setup
    setContentView(R.layout.activity_restricted)
    // ... your regular initialization logic
}

Bonus: Intercept Button Navigations Early

If users try to open restricted Activities via button taps, you can add the check directly in the button’s click listener for a smoother experience (instead of letting the Activity open then close):

btnOpenRestricted.setOnClickListener {
    if (!AppFirstRunManager.isFirstRun(this)) {
        // Allow navigation only after first-run setup is done
        startActivity(Intent(this, RestrictedActivity::class.java))
    } else {
        // Show a message or redirect to setup
        Toast.makeText(this, "Please complete the initial setup first!", Toast.LENGTH_SHORT).show()
        startActivity(Intent(this, OnboardingActivity::class.java))
    }
}

Quick Notes

  • If a user clears your app’s data, SharedPreferences will be erased, and the app will treat it as a first run again—this is expected behavior.
  • If your app has other entry points (like deep links or notifications that open restricted Activities), make sure to add the check in those Activities’ onCreate() to cover all cases.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:25:38