I am showing an alert box in my app to enable GPS, app works fine for the first time but crashes on second time, then works fine for third and crashes for fourth time and so on. When crashes it shows error "Is your activity running?". I tried it with handler and separate UI thread, but no success. I am trying this.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExceptionHandler.register(this, WebServiceIface.BASE_URL
+ "/exception/log");
instanceState = savedInstanceState;
appStatus = AppStatus.getInstance(this);
appStatus.mFromQrCode = false;
mhandler = new Handler();
appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isFromLogin = getIntent().getBooleanExtra("LOGIN_FLAG", false);
isOnPause = false;
listenForLocation();
loginIfNeeded();
}
private void listenForLocation() {
gps = GpsLocator.getGpsLocator(this);
gps.listen();
}
@Override
protected void onResume() {
super.onResume();
gps.destroyGpsDialog();
listenForLocation();
compareTimes();
isResuming = true;
isOnPause = false;
isFromRefresh=true;
}
I am calling alert box from GpsLocator Activity
public void createGpsDialog(){
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?").setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.dismiss();
context.startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),6186);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.dismiss();
}
});
alert = builder.create();
alert.show();
}
It is very weird that it is crashing only on second time
EDIT..
private GpsLocator(CheckinNativeActivity context) {
this.context = context;
this.observers = new ArrayList<GpsFragment>();
this.removalList = new ArrayList<GpsFragment>();
}
public static GpsLocator getGpsLocator(CheckinNativeActivity cxt) {
if (ref == null)
ref = new GpsLocator(cxt);
return ref;
}