0

Suppose I have code like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_msmap);
    setUpMapIfNeeded();
    if (mMap == null) {
      return;
    }

Before I start the activity I have checked google maps libraries are available. However, if there is no sim card / internet connectivity mMap still returns null.

One would think this works since the Google maps / Google play actually shows an error with "not available" with an OK button. But if one clicks it, the app hangs...

So rather, when mMap == null, I would like to simply exit the activity constructor showing an error, and then return to the prior activity. Is there any way to do that gracefully in some way?

Tom
  • 3,587
  • 9
  • 69
  • 124
  • 4
    Try calling finish() when you want to exit – BobTheBuilder Apr 09 '13 at 14:24
  • I think there must be somehing unusual with with the maps API. Now I immediately get the "app hangs" dialog (which I before first got efter clicking the Google Play "ok" button). – Tom Apr 09 '13 at 16:46
  • Nevermind. I just need to use return; after finish() of course. It works! thanks :) – Tom Apr 09 '13 at 17:51

4 Answers4

1

Calling finish() would exit the Activity.

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_msmap);
  setUpMapIfNeeded();
  if (mMap == null) {
    finish();
  }
}
jaibatrik
  • 6,770
  • 9
  • 33
  • 62
  • 1
    I think this was the first response, so I set this as the right answer. (Just a quick note: To avoid having rest of the method executed, one should still call return; as well.) – Tom Apr 09 '13 at 17:54
1

What about calling finish() within the activity, e.g.

if (mMap== null){
    finish();
}
blackpanther
  • 10,998
  • 11
  • 48
  • 78
1

Try calling finish() when you want to exit:

  if (mMap == null) {
    finish();
  }
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_msmap);
    setUpMapIfNeeded();
    if (mMap == null) {
        AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
        dlgAlert.setMessage("Whatever your error is");
        dlgAlert.setTitle("Title");
        dlgAlert.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            finish();
            }
        });
        dlgAlert.create().show();
    }
}
Anirudha Agashe
  • 3,510
  • 2
  • 32
  • 47