Search code examples
androidandroid-activityandroid-wifi

Restart activity after user turn wifi on


I override onReceivedError to create a dialog to let the user turn on the wifi if it is off.

My code there is something like:

if (! mWifi.isConnected() ) {

  Intent i = new Intent(Settings.ACTION_WIFI_SETTINGS);
  startActivity(i);
}

This open the wifi settings, and after the user turn on wifi and hit back.... What i need to add to restart the main activity?


Solution

  • Launch Settings.ACTION_WIFI_SETTINGS using startActivityForResult instead of startActivity because when user return back then onActivityResult method called in Activity where you can check again is wifi is enabled or not before restarting Activity.

    Start WIFI setting as:

    startActivityForResult(new Intent(Settings.ACTION_WIFI_SETTINGS),0);
    

    Override onActivityResult method in Activity as:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        if(requestCode==0)
        {
               WifiManager wifiManager = (WifiManager) 
                                         getSystemService(Context.WIFI_SERVICE);
            if(!wifiManager.isWifiEnabled())
               //restart Application here
        }    
    }