1

I have a dialog that checks to see if the internet is on. If it isn't on, I display a pop-up giving you the option to turn data on or exit the app. If you choose to turn data on it will take you to the settings page to turn data on. Here is the problem: When you press back from the settings page the dialog is still present, it doesn't get dismissed. Here is the code:

public void calldialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(
            "You need to enable an internet connection in order to use this Chirps:")
            .setCancelable(true)
            .setPositiveButton("Turn on Data",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            Intent newintent = new Intent(
                                    android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                            newintent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                            startActivity(newintent);
                            dialog.dismiss();
                        }
                    })
            .setNegativeButton("Exit",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Main.this.finish();
                        }
                    });
    builder.show();

}

When I only had one option the dialog functioned properly. But as soon as I add the second option it causes the button to show twice. The internet check function in which the the calldialog() is contained is called in both the oncreate and onresume which may be part of the problem.

Nick
  • 9,285
  • 33
  • 104
  • 147

2 Answers2

4

You call startActivity() before dialog.dismiss(). This gives control to the new activity.

Zack Marrapese
  • 12,072
  • 9
  • 51
  • 69
3

First of all, when you start your app in android the activitysequens are as follows:

activity starts --> onCreate() is called --> onResume() is called --> activity is running.

When you go from setting and back to your app onResume() is called, so you only need to make the check in onResume().

CornflakesDK
  • 649
  • 7
  • 15
  • This also was helpful but for another problem I was having related to my question :). Thanks. – Nick Aug 26 '11 at 14:38
  • Should setting check such as Internet or Location be done in onCreate() or in onResume() ? – charany1 Apr 03 '16 at 11:40