I have an activity in in my app which I have kept as

android:enabled="false"

now I want that when a button is clicked it should get changer to

android:enabled="true"

How can I do this?

up vote 21 down vote accepted

Here's an example how to enable/disable an Activity:

    PackageManager pm = getPackageManager(); 
    pm.setComponentEnabledSetting(new ComponentName(this, com.packagename.MyActivity.class),
                                  PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

    pm.setComponentEnabledSetting(new ComponentName(this, com.packagename.MyActivity.class),
                                  PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
  • I have put this under my button's onClick method and I am getting error saying "the constructor ComponentName( ) is undefined – creepy_driver Jul 5 '13 at 17:33
  • 2
    this in the constructor is a context so you'll have to replace it with "myactivity.this" where myactivity is the activity your button belongs to – Emanuel Moecklin Jul 5 '13 at 17:38

Here's a nice way to do it:

  public static void setActivityEnabled(Context context,final Class<? extends Activity> activityClass,final boolean enable)
    {
    final PackageManager pm=context.getPackageManager();
    final int enableFlag=enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    pm.setComponentEnabledSetting(new ComponentName(context,activityClass),enableFlag,PackageManager.DONT_KILL_APP);
    }

example usage:

setActivityEnabled(this,SomeActivity.class,false);

Use PackageManager and setComponentEnabledSetting() to enable or disable any component within your app: activity, service, etc.

  • So I can do this only when a button gets clicked right? – creepy_driver Jul 5 '13 at 17:38
  • @VyprNoch: You can do it whenever you feel like. :-) – CommonsWare Jul 5 '13 at 18:23
  • @CommonsWare : Being a system application can I disable components from other packages or the package itself? Or does it require to have system signature? – Saty Feb 1 '17 at 5:50
  • @Saty: Off the top of my head, I do not know -- sorry! – CommonsWare Feb 1 '17 at 12:30
  • @CommonsWare I am enabling & disabling activities well as mentioned methods. But in some phones, app goes in background which gives wired experience. – Aman Srii Sep 28 '17 at 12:54

In case you're using productFlavors, you can have separate AndroidManifest files for each flavor. In those you can easily override properties from the main Manifest file. When building a particular flavor manifest merger merges the Manifest based on the selected flavor.

In the flavor's manifest where you want to disable a particular activit you can do it like this

    <activity
        android:name="com.facebook.CustomTabActivity"
        android:enabled="false"
        tools:node="remove">
    </activity>

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.