'Hiding and Unhiding App icon in the launcher

I am currently implementing a feature to hide and unhide app icon in the launcher based on some certain condition.

I have succeeded in hiding the icon. However, I am finding it difficult to bring the app icon back except that I re-install the application.

Is there a way to make it possible to hide and unhide without having to re-install the application.

To hide

       findViewById<TextView>(R.id.txt).setOnClickListener {
            val p: PackageManager = packageManager
            val componentName = ComponentName(
                this,
                MainActivity::class.java
            ) // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />

            p.setComponentEnabledSetting(
                componentName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )
        }

To show

       findViewById<TextView>(R.id.txt).setOnClickListener {
            val p: PackageManager = packageManager
            val componentName = ComponentName(
                this,
                MainActivity::class.java
            ) // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />

            p.setComponentEnabledSetting(
                componentName,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP
            )
        }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source