Android: stop activity appearing when an app is reopened?

Posted by AP257 on Stack Overflow See other posts from Stack Overflow or by AP257
Published on 2010-04-17T15:41:22Z Indexed on 2010/04/17 15:43 UTC
Read the original article Hit count: 183

Filed under:

Hi there

In Android, I have some code to check whether the user has GPS switched on, and launch the Settings for them to turn it on if they don't. It looks like this:

private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
    .setMessage(
    "Your GPS seems to be disabled - you need it to get a location fix. Turn it on now?")
    .setCancelable(false).setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
        public void onClick(
                @SuppressWarnings("unused") final DialogInterface dialog,
                @SuppressWarnings("unused") final int id) {
            Intent j = new Intent();
            j.setAction("android.settings.LOCATION_SOURCE_SETTINGS");
            startActivity(j);
        }
    }).setNegativeButton("No",
            new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog,
                @SuppressWarnings("unused") final int id) {
            dialog.cancel();
        }
    });
    final AlertDialog alert = builder.create();
    alert.show();
}

However, I'm finding that if the user opens the Settings, turns GPS on, then carries on using the app as normal, there is an odd problem. Often, when the user reopens the app, the Settings are at the front. How can I set them so they don't keep reappearing?

I wonder if I should be using the CLEAR_TOP flag or something similar... I've tried looking at the docs for the Activity flags but find them a bit confusing. Anyone know?

© Stack Overflow or respective owner

Related posts about android