Search code examples
androidandroid-studiokotlinandroid-wifi

how to detect the closing of the app and take acction ? ( kotlin )


I want to disable the WiFi when the app is closed. i know the code to disable WiFi using this line :

wifiManager!!.isWifiEnabled = false

but i don't know how to detect the closing of the app.


Solution

  • This exactly what lifecycles are used for. Any clean up work that needs to done should be done in onDestroy(). This is the final call you receive before your activity is destroyed. So in the activity where you want to disable wifi you can just do:

    override func onDestroy() {
    
       super.onDestroy();
       wifiManager!!.isWifiEnabled = false;
    
    }