Sometimes, you might want an activity to be available only under specific conditions. But you may want to completely disable it at runtime when some conditions are met. For instance based on the time, or the user geolocation.
This could also let one have different activities based on the user's configuration
You can do so simply by using the PackageManager. In the following example, I disable an activity and enable another one.
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, ActivityC.class),
pm.setComponentEnabledSetting(new ComponentName(this, ActivityA.class),
The manifest declares the two activities with the same intent filter, namely the home screen shortcut. But using the previous code, I'm able to get a single one of them in the launcher menu at any given time.
In one of my application, I want to perform an action when the user has been idle for more than n minutes.
While searching the web I found those two callbacks in the Activity class : onUserInteraction() and onUserLeaveHint , both available since Cupcake.
The first one is simply called whenever an interaction is consumed by the activity : touch, key event, trackball event. The second is called just before onPause() when the user leaves your Activity on purpose. For example by pressing the home button, launching another activity from the notification bar. It won't be called when an Activity generated by the system comes in the foreground (an incoming phone call for instance).
And there are a lot more callbacks in an Activity that one can override. So, after some extensive logging and reading through AOSP sources, here's a new and more complete Activity lifecycle graph, with all the callbacks one may find usefull. I also included the callbacks sent to an Activity's fragments.
Feel free to add any remark to make this diagram as accurate as possible.