admin管理员组

文章数量:1389768

I have an Android application with a multi-process architecture where certain activities are launched in different processes. Here is the scenario:

  • Main Activity (Activity A) runs in Process A.
  • When a user clicks on items listed in Activity A, a new Activity B is spawned in a different Process B, and the user is redirected there.

Edit: There can be independent App invocations of Activity B other than getting launched from Activity A.

I need to show a specific dialog once per cold boot/session, regardless of the number of processes the app spawns. The dialog should be shown when certain conditions are met, but the process/activity where the user is present when these conditions are met cannot be predicted.

I can use Shared Preferences for this to save a setting (e.g., DIALOG_SHOWN) to indicate that the dialog has been shown and should not be shown again in the same session. This preference needs to be reset to false during every app boot to ensure the dialog is shown once per cold boot.

In a single-process app, I could reset the preference in Application::onCreate or another callback guaranteed to execute once per cold boot. However, in a multi-process architecture, each process creates its own instance of the Application object. Therefore, if I put the reset logic in Application::onCreate, the preference would be wrongly reset to false when a new process (e.g., Process B) is spawned, causing the dialog to be shown again.

I tried using static variables, but they are created afresh for every process.

Are there any other ways in Android to execute something "only" once per cold boot of the app (not the device), irrespective of the number of processes it ends up spawning?

本文标签: multiprocessAndroid Execute code once per cold boot of android appStack Overflow