0

In my code, I am getting the user's location using FusedLocationProviderClient. In the callback, I am saving the latitude and longitude of the user on Firebase real time database. Everything works fine even when the user moves from one place to another, the only problem is, every time it saves the coordinates on Firebase, the activity refreshes, how do I stop this automatic refreshing?

I am posting only the onCreate method and the call back method, if more code is needed, I will provide it.

Everything is working fine in my code except the activity refreshes every time new coordinates are saved on Firebase?

Note: My code is in a Fragment

private static final String TAG = "DriverMapFragment";
    int LOCATION_REQUEST_CODE = 10001;
    FusedLocationProviderClient fusedLocationProviderClient;
    LocationRequest locationRequest;
    double latitude,longitude;
    DatabaseReference databaseReference;
    String schoolName, driverPhone, vehicleNumberPlate;
    TextView newLat,newLng;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_driver_map, container, false);

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
        locationRequest = LocationRequest.create();
        locationRequest.setInterval(4000);
        locationRequest.setFastestInterval(2000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        databaseReference = FirebaseDatabase.getInstance().getReference();
        Intent intent = getActivity().getIntent();
        schoolName = intent.getStringExtra("SchoolName");
        driverPhone = intent.getStringExtra("DriverPhone");
        vehicleNumberPlate =       intent.getStringExtra("VehicleNumberPlate");
        newLat = view.findViewById(R.id.newLat);
        newLng = view.findViewById(R.id.newLng);




        return view;
    }
    
    
    
    LocationCallback locationCallback = new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null){
                return;
            }
            for (Location location : locationResult.getLocations()){
                Log.d(TAG,"onLocationResult: " + location.toString());
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                Log.i("Lat",String.valueOf(latitude));
                Log.i("Lng",String.valueOf(longitude));
                


                LocationHelper helper = new LocationHelper(
                        location.getLongitude(),
                        location.getLatitude()
                );

                FirebaseDatabase.getInstance().getReference().child("Schools")
                        .child(schoolName)
                        .child("drivers")
                        .child(vehicleNumberPlate)
                        .child("driverLocation")
                        .setValue(helper).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {

                        if(task.isSuccessful()){
                            Log.i("Success","Location Saved");
                        }
                        else{
                            Log.i("Failure","Location Not Saved");
                        }
                    }
                });



            }
        }
    };
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

0

Make sure you use addListenerForSingleValueEvent instead of addValueEventListener, otherwise when you update data to Firebase, addValueEventListener will be called again and run the Activity you called in.