views:

63

answers:

1

Hi,

when the device is not connected to the internet, I send the user to the settings

startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS)

but then I'm stuck in the settings page. How can I go back to my main activity?

thanks

Jul

+1  A: 

In that case, the user will have to explicitly go back to your activity (e.g. by pressing the back button a couple of times). I usually do this on my first Activity in those kind of situations:

@Override
public void onResume(){
    super.onResume();
    // first, check connectivity
    if ( isOnline ){
        // do things if it there's network connection
    }else{
        // as it seems there's no Internet connection
        // ask the user to activate it
        new AlertDialog.Builder(SplashScreen.this)
            .setTitle("Connection failed")
            .setMessage("This application requires network access. Please, enable " +
                    "mobile network or Wi-Fi.")
            .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // THIS IS WHAT YOU ARE DOING, Jul
                    SplashScreen.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SplashScreen.this.finish();
                }
            })
            .show();
    }
}

As you can see, I check the Internet connection in the onResume method, so that it will check if it user activated the WiFi or not.

Cristian
Great, thank you. Once the wifi is enabled, I still have to click the back button to get back to the main activity, but it's ok for me.
jul