Please check the code

I wrote this code for alarm. It’s the same one as in the video. But unlike in video, if after clicking the button, I remove the app from recent activities, the alarm doesn’t works i.e. MainActivity2 doesn’t starts after 1 minute. But, if the app remains in recent activities, it works fine. What may be the possible reason behind this?

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

    btnTask.setOnClickListener {

        val i = Intent(this,MainActivity2::class.java)

        val pi = PendingIntent.getActivity(this, 1234, i, PendingIntent.FLAG_ONE_SHOT)
        val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

        alarmManager.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime() + 60000, pi)

    }

}

}

Alarm manager doesn’t work in most cases either you need to use work manager or follow this https://github.com/yuriykulikov/AlarmClock