1

I am getting this error when i exit this activity and go to another activity.By searchng this error,i understood that we exit the activity while the background activity comes back with the result.I tried to finish the activity in onbackpressed() method but did not work.I read in some post that if we use this code if(!((Activity) context).isFinishing()){ //show dialog } it will work.But i got no clue where to use it,also why we would we use some dialog in the code if we dont want to show.

    public class LocationActivity extends Activity implements LocationListener {


         private final String TAG = getClass().getSimpleName();

         protected ConnectionDetector connectionDetector;
         public Boolean isInternetPresent;
         private String[] places;
         public static Loc [] getloc;
         private LocationManager locationManager;
         private Location loc;
         static HttpEntity entityResponse = null;
         GoogleMap map;
         MarkerOptions mp;
         Activity activity;
         Context context;

         Double lat,lon;
        final   String Googlekey=" " ;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

SharedPreferences sharedPreferences;
            int locationCount = 0;
            connectionDetector = new    ConnectionDetector(getApplicationContext());
            isInternetPresent = connectionDetector.isConnectingToInternet();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location);

         LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

          lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);

          map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();



        }

        @Override
        public void onLocationChanged(Location location) {


            map.clear();

            lat=location.getLatitude();
            lon=location.getLongitude();
             Googleapicall exeTask = new Googleapicall();
              exeTask.execute();
              mp = new MarkerOptions();

               mp.position(new LatLng(lat, lon));
               map.addMarker(mp);
               mp.title("My Location");

             map.animateCamera(CameraUpdateFactory.newLatLngZoom(
              new LatLng(lat, lon), 13));


        }



        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)      {

        }

        public class Googleapicall extends AsyncTask<String, Void, String>

        {


            Loc statStr = null;
             Loc[] statInfoResult = null;
             ArrayList<Loc> SIArrayList = new ArrayList<Loc>();

            private ProgressDialog Dialog;

            @Override
            protected void onPreExecute() {
                Dialog = new ProgressDialog(LocationActivity.this);
                Dialog.setMessage(LocationActivity.this.getResources().getString(
                        R.string.loading));
                Dialog.setCancelable(false);
                Dialog.show();
                }

            @Override
            protected String doInBackground(String... params) {

                String result = "";
                try 
                {

                    if(isInternetPresent)

                    {
                    getloc=Webcall.getdet(lat,lon,Googlekey);
                    result = "Success";
                    System.out.println("getloc "+getloc.length);

                    }


                    else 
                    {
                        result = "Failure";
                    }
                } 


                catch (Exception e)
                {
                    result = "Failure";
                    e.printStackTrace();
                }

                return result;

            }

        @Override
            protected void onPostExecute(String result) {


                Loc lc=new Loc();

                try {

                    if(result.contains("Success"))
                    {

                        mp=new MarkerOptions();

                        for(int i=0;i<getloc.length;i++)
                        {
                            Double lati=getloc[i].latitudes;
                            Double longi=getloc[i].longitudes;

                               map.addMarker(new MarkerOptions()
                                  .title("Nearbyplace")
                                  .position(
                                    new LatLng(lati,longi))
                                  .icon(BitmapDescriptorFactory.fromResource(R.drawable.small)));
                        }
                    }

                    } 
                catch 
                (Exception e) 
                {
                    e.printStackTrace();    

                }

                Dialog.dismiss();

            }

}

        @Override
        public void onBackPressed() {

            super.onBackPressed();

            Intent i = new Intent(LocationActivity.this, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            LocationActivity.this.startActivity(i);
            ((Activity) LocationActivity.this).finish();
        }

    }
Hari Sankar
  • 197
  • 2
  • 15
  • Check out the answers to this question: http://stackoverflow.com/questions/10379134/finish-an-activity-from-another-activity?answertab=votes#tab-top. You will be able to find one that suits your particular use case. – Kay_N Apr 08 '15 at 02:14
  • Btw you do NOT have to use the dialogue method, if you don't want to. You can use toast instead or use another approach altogether. – Kay_N Apr 08 '15 at 02:16
  • 1
    Check ``LocationActivity.this.isFinishing()`` before showing dialog – joao2fast4u Jul 20 '16 at 10:57

1 Answers1

0

Use it onPreExecute() method

@Override
protected void onPreExecute() {
    Dialog = new ProgressDialog(LocationActivity.this);
    Dialog.setMessage(LocationActivity.this.getResources().getString(
            R.string.loading));
    Dialog.setCancelable(false);
    if (!((Activity) context).isFinishing()) {
        Dialog.show();
    }
}
Praful C
  • 162
  • 1
  • 3
  • 14