Activity refresh / "Performing a stop operation that has not been resumed"
I am trying to update my activity after enabling GPS with an alertdialog that opens GPS settings. I have implemented the onResume function, but I end up with this on my logcat:
performing stop of activity that is not resumed(...)
I have looked at this post , but I cannot figure out what is wrong with my code (or my onResume function) and how to update my activity after coming back from GPS settings.
Could you help me? here is my MainActivity.java
source to share
When calling
gps.showSettingsAlert();//inside this use startActivityForResult(new Intent().setClass(this, MainActivity.class), result_code);
Edit: Your method must be in MainActivity.class and not in the service, since the service has no ui, so there is no point in getting the result.
public void showSettingsAlert(Context context){
AlertDialog.Builder alertdialog = new AlertDialog.Builder(context);
alertdialog.setTitle("Oups ! pas de données GPS");
alertdialog.setMessage("GPS n'est pas activé sur votre appareil, voulez vous l'activer ?");
/*
handling the buttons :
*/
alertdialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, RESULT_CODE_GPS);
}
});
// on pressing cancel button
alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertdialog.show();
}
Here is your activity result method, which will be called after the user clicks on the settings page.
@SuppressLint("NewApi") @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == RESULT_CODE_GPS && resultCode == 0){
// @SuppressWarnings("deprecation")
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_MODE);
if(provider != null && !provider.isEmpty()){
Log.v("mAINaBBB", " Location providers: "+provider);
//Start searching for location and update the location text when update available.
// Do whatever you want
}else{
Log.v(, "Nothing put on");
//Users did not switch on the GPS
}
}
}
Now in MainAcitivity the method you called before
if(gps.canGetlocation() ){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
device_location = new ParseGeoPoint(latitude,longitude);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
showSettingsAlert(MainActivity.this); //**This line should be changed then**
}
source to share
In your MainActivity.onResume (), you are trying to start a new MainActivity.
@Override
protected void onResume() {
super.onResume();
Intent refresh =new Intent(this, MainActivity.class);
startActivity(refresh);
}
This is just a bad idea and will give an infinite loop in which MainActivity is constantly starting a new MainActivity.
Just remove the onResume () and update the views of the current activity, not create new ones.
source to share