Show ProgressDialog after Back Button Press

954 Views Asked by At

I have pressed Back Button either after navigation start or after route shown.

I am working on Fragment. View be like in order: A-B-C-D in the Fragment. And, Back Press work flow is like : B:A, C:B:A, D:C:B:A.

But, I want to use progress bar after every back press before displaying the screen where I came after back press.

I have used the following code in onBackPress method:

public boolean onBackPressed() {
    if (hasNavigationStarted) {
        AlertDialog.Builder alert = new AlertDialog.Builder(HomeFragment.this.getActivity());
        alert.setTitle("Really quit?");
        alert.setMessage("Do you want to exit navigation?");
        alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {
                isDrivingModeOn = false;
                navigationManager.stopNavigation();
                corLDriveInfoContainer.setVisibility(View.VISIBLE);
                mapView.clearAllOverlays();

                progressBar = new ProgressDialog(getActivity());
                progressBar.setMessage("Loading ...");
                progressBar.show();
                progressBar.setCancelable(false);
                if (isDrivingDirectionsOn) {
                    showDriveInfoForDrivingDirection();
                    rlDrivingDirections.setVisibility(View.VISIBLE);
                    ((ToolbarActivity) getActivity()).getSupportActionBar().setTitle(R.string.app_name);
                    if (drivingDirectionDestPoint != null) {
                        mapView.deleteAllAnnotationsAndCustomPOIs();
                        addAnnotation(drivingDirectionDestPoint);
                        mapView.centerMapOnPosition(new SKCoordinate(drivingDirectionDestPoint.getLongitude(), drivingDirectionDestPoint.getLatitude()));
                    }
                    if (drivingDirectionStartPoint != null) {
                        mapView.deleteAllAnnotationsAndCustomPOIs();
                        addAnnotation(drivingDirectionStartPoint);
                    }
                } else if (latestDriveInfoPlace != null) {
                    setSearchViewShown(true);
                    addAnnotation(latestDriveInfoPlace);
                    mapView.setZoom(MapUtils.DEFAULT_ZOOM_VALUE);
                    mapView.centerMapOnPosition(new SKCoordinate(latestDriveInfoPlace.getLongitude(), latestDriveInfoPlace.getLatitude()));
                    searchItem.expandActionView();
                    searchView.setQuery(latestDriveInfoPlace.getName(), false);
                    searchView.clearFocus();
                }
            }
        });
        alert.setNegativeButton("Cancel", null);
        alert.show();
        return true;
    } else if (isRouteShown) {
        isDrivingModeOn = false;
        navigationManager.removeRouteCalculationViews();
        mapView.deleteAllAnnotationsAndCustomPOIs();
        isRouteShown = false;
        corLDriveInfoContainer.setVisibility(View.VISIBLE);

        if (isDrivingDirectionsOn) {
            rlDrivingDirections.setVisibility(View.VISIBLE);
            ((ToolbarActivity) getActivity()).getSupportActionBar().setTitle(R.string.app_name);
            if (drivingDirectionDestPoint != null) {
                mapView.deleteAllAnnotationsAndCustomPOIs();
                addAnnotation(drivingDirectionDestPoint);
                mapView.centerMapOnPosition(new SKCoordinate(drivingDirectionDestPoint.getLongitude(), drivingDirectionDestPoint.getLatitude()));
            }
            if (drivingDirectionStartPoint != null) {
                mapView.deleteAllAnnotationsAndCustomPOIs();
                addAnnotation(drivingDirectionStartPoint);
            }
        } else if (latestDriveInfoPlace != null) {
            setSearchViewShown(true);
            addAnnotation(latestDriveInfoPlace);
            mapView.setZoom(MapUtils.DEFAULT_ZOOM_VALUE);
            mapView.centerMapOnPosition(new SKCoordinate(latestDriveInfoPlace.getLongitude(), latestDriveInfoPlace.getLatitude()));
            searchItem.expandActionView();
            searchView.setQuery(latestDriveInfoPlace.getName(), false);
            searchView.clearFocus();
        }
        return true;
    } else if (isDrivingDirectionsOn) {
        isDrivingDirectionsOn = false;
        rlDrivingDirections.setVisibility(View.GONE);
        corLDriveInfoContainer.setVisibility(View.GONE);
        setSearchViewShown(true);
        searchItem.collapseActionView();
        drivingDirectionDestPoint = null;
        drivingDirectionStartPoint = null;
        atvDestination.setText("");
        atvStart.setText("");
        hideKeyboard();
        ((DrawerViewInterface) getActivity()).showHamburgerIcon();
        ((DrawerViewInterface) getActivity()).unlockDrawer();
        mapView.deleteAllAnnotationsAndCustomPOIs();
        return true;
    }
    return false;
}

Update code

I have implemented the ProgressDialog.

progressBar = new ProgressDialog(getActivity());
progressBar.setMessage("Loading ...");
progressBar.show();
progressBar.setCancelable(false);

But, Where do I use the dismiss() method here after returning the screen when back press ? What can be done to solve this issue ?

3

There are 3 best solutions below

1
On

You want to show your ProgressDialog when some task (that takes some time to finish) starts, and dismiss it when the task finishes.

I can't see such task in your code snippet, but if there is one I believe it provides an API to register a listener in which you should show and dismiss your ProgressDialog.

If you don't have a task that takes some time to finish, it is not good practice to show the ProgressDialog. Try a different approach. Maybe some animations (eg. fade out views, change data, fade in views).

0
On

You can use the progress in onResume and dismiss it in the onMapReady callback.

In your fragments onResume:

if(mMap == null){
    progressBar = new ProgressDialog(getActivity());
    progressBar.setMessage("Loading ...");
    progressBar.show();
    progressBar.setCancelable(false);
}

and in your onMapReady:

if(progressBar != null && progressBar.isShowing()){
    progressBar.dismiss();
}
0
On

Try something like this in each fragment:

 @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressBar = new ProgressDialog(getActivity());
        progressBar.setMessage("Loading ...");
        progressBar.show();
        progressBar.setCancelable(false);


    }

    @Override
    protected Boolean doInBackground(Void... params) {

    //Network call to refresh map data


         }

    @Override
    protected void onPostExecute(final Boolean success) {
       if(success)
            progressBar.dismiss();

}