按后退按钮显示ProgressDialog

时间:2017-01-03 07:39:17

标签: android android-studio android-fragments progressdialog

我在Back Button之后或navigation start之后按了route shown

我正在研究FragmentView按顺序排列:Fragment中的A-B-C-D。并且,Back Press工作流程如下:B:A,C:B:A,D:C:B:A。

但是,我希望在每progress bar之后使用back press,然后再显示我back press之后的屏幕。

我在onBackPress方法中使用了以下代码:

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;
}

更新代码

我已实施ProgressDialog

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

但是,在dismiss()返回屏幕后,我在哪里使用back press方法?可以做些什么来解决这个问题?

3 个答案:

答案 0 :(得分:0)

当某个任务(需要一些时间才能完成)启动时,您希望显示ProgressDialog,并在任务完成时将其关闭。

我在你的代码片段中看不到这样的任务,但如果有的话,我相信它提供了一个API来注册你应该显示和解除你的ProgressDialog的监听器。

如果您没有完成任务需要一些时间,那么显示ProgressDialog并不是一个好习惯。尝试不同的方法。也许是一些动画(例如,淡出视图,更改数据,淡入视图)。

答案 1 :(得分:0)

在每个片段中尝试这样的事情:

 @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();

}

答案 2 :(得分:0)

您可以使用onResume中的进度并在onMapReady回调中将其关闭。

在片段onResume:

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

和你的onMapReady:

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