javaandroidandroid-intentgoogle-maps-api-3long-click

Disable Reoplening Activitiy Mulitple Times on Map Long Clicks


I am making an app that enables a user to long click on map, and open a new activity that would allow them to add a new tag and info about it. When user long clicks once, if he is fast enough he can long click on the map twice, and the second activity would open twice. I am trying to find a way to disable this behavior. I already tried some examples, and tried to add flags, but it had no effect.

I would like to disable user to long click twice. I would also like to maybe add a loader.

In short, what I want to do is: Disable long click if user already long clicked to open the new activity, and enable it again when the new activity is closed.

My maps fragment looks like this:

 //Add marker on long click
 mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

     @Override
     public void onMapLongClick(final LatLng arg0) {

         RequestQueue queue = Volley.newRequestQueue(getActivity());
                    String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey";

         // Request a string response from the provided URL.
         StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {
                 try {
                     JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");

                     Intent intent = new Intent(getActivity(), AddRestaurantActivity.class);

                      for (int i = 0; i < jObj.length(); i++) {
                          String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
                          if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route")
                                                    || componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2")
                                                    || componentName.equals("administrative_area_level_1") || componentName.equals("country")) {
                                                intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name"));
                           }
                      }

                       intent.putExtra("latitude", arg0.latitude);
                       intent.putExtra("longitude", arg0.longitude);

                       startActivity(intent);

                   } catch (JSONException e) {
              e.printStackTrace();
              }
          }
    }, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        int x = 1;
    }
 });
 // Add the request to the RequestQueue.
 queue.add(stringRequest);

    }
});

And this is the activity it opened, with this and this answers tried (among others) to add a flag:

private void setRestaurant(final String userId, final String message, final String pickDate, final String pickTime, final String location, final String lat, final String lon, final String sendTo, final boolean enableComments) {
    // Tag used to cancel the request
    String tag_string_req = "req_add_restaurant";

    final String commentsEnabled = (enableComments) ? "0" : "1";

    pDialog.setMessage(getString(R.string.setting_a_restaurant));
    showDialog();

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    Call<DefaultResponse> call = apiService.addrestaurant(userId, message, lat, lon, pickDate, pickTime, sendTo, commentsEnabled);
    call.enqueue(new Callback<DefaultResponse>() {
        @Override
        public void onResponse(Call<DefaultResponse> call, retrofit2.Response<DefaultResponse> response) {

            // Launch main activity
            Intent intent = new Intent(SetRestaurantActivity.this,
                    MainActivity.class);
            // I TRIED TO BLOCK IT HERE
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            // I ALSO TRIED: 
            // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            finish();

            Toast.makeText(getApplicationContext(), R.string.sucessfully_created_restaurant, Toast.LENGTH_LONG).show();
        }
    });
}

Solution

  • Simple add flag variable. In this case i am using isRequestProcess variable for that.

    Boolean isRequestProcess = false;
    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
    
     @Override
     public void onMapLongClick(final LatLng arg0) {
         if(isRequestProcess){
             return;
         }
        isRequestProcess = true;
         RequestQueue queue = Volley.newRequestQueue(getActivity());
                    String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey";
    
         // Request a string response from the provided URL.
         StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {
                 try {
                     JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");
    
                     Intent intent = new Intent(getActivity(), AddRestaurantActivity.class);
    
                      for (int i = 0; i < jObj.length(); i++) {
                          String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
                          if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route")
                                                    || componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2")
                                                    || componentName.equals("administrative_area_level_1") || componentName.equals("country")) {
                                                intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name"));
                           }
                      }
    
                       intent.putExtra("latitude", arg0.latitude);
                       intent.putExtra("longitude", arg0.longitude);
    
                       startActivity(intent);
                       isRequestProcess = false;
    
                   } catch (JSONException e) {
                      e.printStackTrace();
                  }
            }, new Response.ErrorListener() {
    
                @Override
                public void onErrorResponse(VolleyError error) {
                    int x = 1;
                }
    
            }
        }
    }