我有一个活动,通过显示一个带有OK和CANCEL按钮的对话框,请求用户在onStart()中启用GPS。但是,如果用户在与对话框交互之前按下主页按钮,然后再次启动应用程序,则会在旧对话框的顶部出现一个新对话框,因此用户会看到两个对话框一个接一个。如何取消活动的onStop方法中的旧对话框?
这是我代码的相关部分:
protected void onStart() {
super.onStart();
enableGPS();
}
private void enableGPS() { //GPS ENABLE RELATED METHOD
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result
.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS: //GPS ALREDY TURNED ON
if (locationServiceConnected) {
if (ActivityCompat.checkSelfPermission(enableGPSActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, enableGPSActivity.this);
}
}
else {
redirectStatus.setText("ERROR: Not able to connect to Location Service");
btnRetry.setVisibility(View.VISIBLE);
}
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: //GPS NOT ALREDY TURNED ON, SHOW TURN ON DIALOG
try {
status.startResolutionForResult(enableGPSActivity.this, REQUEST_CHECK_SETTINGS); //CREATING DIALOG FOR GPS TURN ON
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: //GPS CANNOT BE TURNED ON
redirectStatus.setText("Permission Available, not able to enable GPS");
btnRetry.setVisibility(View.VISIBLE);
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { //GPS ENABLE RELATED METHOD, DIALOG FOR GPS TURN ON
if (requestCode == REQUEST_CHECK_SETTINGS) {
if (resultCode == Activity.RESULT_OK) { // GPS TURN ON ACCEPTED
if (locationServiceConnected) {
if (ActivityCompat.checkSelfPermission(enableGPSActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
redirectStatus.setText("Please Wait. Fetching Your Current Location");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, enableGPSActivity.this);
}
}
}
if (resultCode == Activity.RESULT_CANCELED) { //GPS TURN ON REJECTED
redirectStatus.setText("Permission Available, GPS Off");
btnRetry.setVisibility(View.VISIBLE);
}
}
}
}
将enableGPS();
调用为onCreate()
而不是onStart()
因为
根据Activity
-的生命周期,当用户按下主页按钮时,当前活动将通过调用-onPause()
和onStop()
方法进入后台从Stack恢复活动时,将通过调用onRestart()
、onStart()
和onResume()
来恢复Activity
。
// declares variables name
private AlertDialog gpsAlertDialog;
private boolean isGpsDialogShowing = false;
private LocationManager manager;
// check gps enable or not if enable than remove dialog else show gps enable dialog in onResume method
protected void onResume()
{
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)&&!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
ShowGpsDialog();
} else {
removeGpsDialog();
}
}
// remove dialog on destroy activity
@Override
protected void onDestroy() {
super.onDestroy();
removeGpsDialog();
}
// method for show dialog for gps enable
private void ShowGpsDialog() {
isGpsDialogShowing = true;
AlertDialog.Builder gpsBuilder = new AlertDialog.Builder(
MainMenuDrawerActivity.this);
gpsBuilder.setCancelable(false);
gpsBuilder
.setTitle(getString(R.string.dialog_no_gps))
.setMessage(getString(R.string.dialog_no_gps_messgae))
.setPositiveButton(getString(R.string.dialog_enable_gps),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// continue with delete
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
removeGpsDialog();
}
})
.setNegativeButton(getString(R.string.dialog_exit),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// do nothing
removeGpsDialog();
finish();
}
});
gpsAlertDialog = gpsBuilder.create();
gpsAlertDialog.show();
}
// method for remove gps dialog
private void removeGpsDialog() {
if (gpsAlertDialog != null && gpsAlertDialog.isShowing()) {
gpsAlertDialog.dismiss();
isGpsDialogShowing = false;
gpsAlertDialog = null;
}
}