【发布时间】: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