onDestroy function completion

Thank you very much for your involvement, K.

I’ve done what you’ve suggested. I found an example of how to use thw WorkManager class, and implemented the code shown below (after adding the ‘androidx.work:work-runtime-ktx:2.4.0’ dependency, of course).

The SendToDevice routine works perfectly when used in other parts of the program. And also, the Log.d(“Debug:”, “On Destroy entered…”) in the onDestroy function executes just fine. But the WorkManager enqueue instruction is not executing at all. That is, neither one of the “Disconnecting…” nor “Disconnected…” messages is ever shown.

The code compiles just fine and reports no errors during program execution.

Also, I tried executing SendToDevice(cmdDisconnect, false) without running it as a Thread and the code also compiled just fine… but it didn’t work either.

class MainActivity : AppCompatActivity() {
    private val closeConnectionRequest: WorkRequest =OneTimeWorkRequestBuilder<CloseConnection>().build()

*
*

    public override fun onDestroy() {
        super.onDestroy()
        if (isDestroyed) {
            Log.d("Debug:", "On Destroy entered...")
            WorkManager.getInstance(mainContext).enqueue(closeConnectionRequest)
        }
    } // onDestroy

    class CloseConnection(appContext: Context, workerParams: WorkerParameters):
        Worker(appContext, workerParams) {
        override fun doWork(): Result {

            val cThread = Thread(SendToDevice(cmdDisconnect, false))
            Log.d("Debug:", "Disconnecting...")
            cThread.start()
            Log.d("Debug:", "Disconnected...")

            // Indicate whether the work finished successfully with the Result
            return Result.success()
        }
    } //CloseConnection

} // MainActivity
1 Like