Jeff Gortmaker
Jeff Gortmaker

Reputation: 4737

Prevent activity from restarting on phone boot?

If a user reboots their phone while my activity is in the foreground, after the phone reboots, the activity automatically pops up again. I don't want this to happen because none of the extra data I pass to the activity is saved, so the activity does not have the correct display.

Apparently the activity is started even before my BroadcastReceiver that has an intent-filter with android.intent.action.BOOT_COMPLETED starts.

How would I go about preventing the activity from automatically starting when the phone boots?

Edit: I use the android.intent.category.HOME category in my intent filter for my activity, which apparently is the reason for it starting up on reboot.

Upvotes: 0

Views: 288

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

The ACTION_SHUTDOWN broadcast is supposed to go out when the device is shutting down. I say "supposed to go out", because it assumes an orderly shutdown. If the user winds up holding the POWER button for ~10 seconds, or popping out the battery, I would assume that ACTION_SHUTDOWN is not broadcast.

To control whether a component (e.g., activity) is available, you can use PackageManager and setComponentEnabledSetting(). A disabled component cannot be run and is generally invisible (e.g., a disabled app widget's <receiver> will not show up in the app widget picker).

In theory, you can combine these two. However, even at the best of times, I would assume that ACTION_SHUTDOWN behaviors are rather time-limited (so be quick about it), and you will need to handle the "disorderly" shutdown scenarios.

Upvotes: 1

Related Questions