【问题标题】:Show ProgressDialog after Back Button Press按下后退按钮后显示 ProgressDialog
【发布时间】:2017-01-03 07:39:16
【问题描述】:

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

我正在处理Fragment。 View 依次为:Fragment 中的 A-B-C-D。而且,Back Press 工作流程就像:B:A、C:B:A、D:C:B:A。

但是,我想在每个back press 之后使用progress bar,然后再显示我在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);

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

【问题讨论】:

  • 你想用进度对话框做什么?
  • 我用的是地图,所以后按地图需要时间刷新,所以我想使用进度对话框。类似于:这种操作:mapView.deleteAllAnnotationsAndCustomPOIs(); addAnnotation(drivingDirectionDestPoint); mapView.centerMapOnPosition(new SKCoordinate(drivingDirectionDestPoint.getLongitude(), drivingDirectionDestPoint.getLatitude())); 发生。所以,我想克服它。
  • 你不必实现那个onBackPress。如果每个片段中都有一个 AsyncTask.. 在 onPreExecute() 上启动对话框并在 PostExecute() 上关闭它。
  • 那么,在on Back Press 中实现进度对话框是个坏主意?
  • 是的.. 因为在 backPress 上,您实际上不知道要等待多长时间才能将数据加载到新片段上。在网络调用之前使用 onPreExecute 中的进度对话框并在网络调用之后在 onPostExecute 中将其关闭是最好的方法。这样您还可以显示进度(如 Loading.... 90%)。

标签: android android-studio android-fragments progressdialog


【解决方案1】:

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

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

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

【讨论】:

  • 有几个任务像:mapView.deleteAllAnnotationsAndCustomPOIs(); addAnnotation(drivingDirectionDestPoint); mapView.centerMapOnPosition(new SKCoordinate(drivingDirectionDestPoint.getLongitude(), drivingDirectionDestPoint.getLatitude())); 后按视图查看。
【解决方案2】:

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

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

}

【讨论】:

    【解决方案3】:

    您可以在 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      相关资源
      最近更新 更多