moveTaskToBack does not work if moveTaskToFront is used once

Solution for moveTaskToBack does not work if moveTaskToFront is used once
is Given Below:

I am using below code in onPause as I want my activity to be in front all the time by disabling the use of all hardware buttons which is working fine.

 <!--Manifest Code-->

<category android:name="android.intent.category.HOME" />
                    <category android:name="android.intent.category.DEFAULT" />

<!-- onPause  -->

     val activityManager = applicationContext
                    .getSystemService(ACTIVITY_SERVICE) as ActivityManager
    
                activityManager.moveTaskToFront(taskId, 0)

But there is a functionality where I need to allow my activity to minimize. And I tried using below code but it did not worked when activity is on front

 val pm = applicationContext.packageManager
            val compName = ComponentName(packageName, "$packageName.screen.activity.LoginActivity")
            pm.setComponentEnabledSetting(
                compName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )

Can anyone suggest in this?