15

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
vishalmullur
  • 1,094
  • 4
  • 14
  • 32

4 Answers4

30

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);
Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • I have put this under my button's onClick method and I am getting error saying "the constructor ComponentName( ) is undefined – vishalmullur Jul 05 '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 05 '13 at 17:38
6

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);
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • How would you enable or disable an activity alias with this? Activity aliases don't appear to have classes associates with them. – Fat Monk Dec 31 '19 at 14:56
  • Have you tried using the exact path to it, using this CTOR : https://developer.android.com/reference/android/content/ComponentName.html#ComponentName(android.content.Context,%20java.lang.String) ? – android developer Dec 31 '19 at 22:35
4

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @VyprNoch: You can do it whenever you feel like. :-) – CommonsWare Jul 05 '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 01 '17 at 05:50
  • @Saty: Off the top of my head, I do not know -- sorry! – CommonsWare Feb 01 '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 Srivastava Sep 28 '17 at 12:54
  • @AmanSrii: Then perhaps you should not enable and disable activities, but instead find some other solution to whatever business problem you are trying to solve. Personally, I have enabled and disabled receivers, but I would not enable or disable an activity. I am not surprised that there are side effects of enabling and disabling activities in some situations. – CommonsWare Sep 28 '17 at 13:29
  • @CommonsWare actually these are launcher activity like splash & mainscreen. Any thought? – Aman Srivastava Sep 28 '17 at 13:45
  • @AmanSrii: I am not surprised that you are running into problems by enabling and disabling such activities. – CommonsWare Sep 28 '17 at 13:47
  • @CommonsWare sorry bother you much. Just want to know app like watsapp changes their launcher and mainscreen smoothly. Any references for help? – Aman Srivastava Sep 28 '17 at 14:05
  • @AmanSrii: I do not use WhatsApp. My assumption is that they have just one activity for those roles, perhaps using fragments to transition between different user interfaces. If you have further concerns in this area, I suggest that you ask a separate Stack Overflow question, where you can explain in detail what you are trying to achieve. – CommonsWare Sep 28 '17 at 15:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155539/discussion-between-aman-srii-and-commonsware). – Aman Srivastava Sep 28 '17 at 17:24
0

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>
Ahsan Zaheer
  • 646
  • 1
  • 12
  • 18