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?
Use PackageManager and setComponentEnabledSetting() to enable or disable any component within your app: activity, service, etc.
activity is the subelement of application and represents an activity that must be defined in the AndroidManifest. xml file. It has many attributes such as label, name, theme, launchMode etc. android:label represents a label i.e. displayed on the screen.
The easiest way is to make MainActivity launcher activity, as usual. Then check in MainActivity#onCreate(Bundle) via SharedPreferences if the user already logged in and, if not, start LoginActivity immediately.
Use PackageManager
and setComponentEnabledSetting()
to enable or disable any component within your app: activity, service, etc.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With