Search code examples
androidandroid-activityandroid-wifi

Right place to disconnet the wifi connection


I am building an App. The app needs to connect to Wifi as and when it starts and it has to close/disconnect the wifi when you come out of the App or when the App crashes.

  1. I need your suggestion on where to place the diconnect command in the Activity.

  2. Is there is any common method which will be called when the App crashes ? If so I can try to fit the Wifi disconnect command there.

Thanks in Advance


Solution

  • Put the disconnect in onPause (if you want it to turn off wifi when the user goes to another app), or onStop (if you want it to turn off only when your activity is ended).

    Nothing is going to be called on a crash, because you're assumed to be in an unrecoverable state.