Android 按“后退”按钮后显示进度对话框
我在Android 按“后退”按钮后显示进度对话框,android,android-studio,android-fragments,progressdialog,Android,Android Studio,Android Fragments,Progressdialog,我在导航开始之后或在显示的路线之后按下了后退按钮 我正在处理片段视图按顺序排列:A-B-C-D在片段中。而且,反压工作流程类似于:B:A,C:B:A,D:C:B:A 但是,我想在每次按下后使用进度条,然后再显示我按下后出现的屏幕 我在onBackPress方法中使用了以下代码: public boolean onBackPressed() { if (hasNavigationStarted) { AlertDialog.Builder alert = new Alert
导航开始
之后或在显示的路线之后按下了后退按钮
我正在处理片段
<代码>视图
按顺序排列:A-B-C-D在片段中
。而且,反压
工作流程类似于:B:A,C:B:A,D:C:B:A
但是,我想在每次按下后使用进度条
,然后再显示我按下后出现的屏幕
我在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);
但是,当返回屏幕并按下时,我在哪里使用dismise()
方法?如何解决此问题?您希望在某项任务(需要一些时间才能完成)开始时显示进度对话框,并在任务完成时将其关闭
我在您的代码片段中看不到这样的任务,但如果有,我相信它提供了一个API来注册侦听器,您应该在其中显示并关闭ProgressDialog
如果您没有需要一些时间才能完成的任务,那么显示ProgressDialog不是一个好的做法。尝试不同的方法。可能是一些动画(例如淡出视图、更改数据、淡入视图)。在每个片段中尝试类似的操作:
@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();
}
您可以在onResume中使用进度,并在onMapReady回调中取消它
在您的简历中:
if(mMap == null){
progressBar = new ProgressDialog(getActivity());
progressBar.setMessage("Loading ...");
progressBar.show();
progressBar.setCancelable(false);
}
在你的自述中:
if(progressBar != null && progressBar.isShowing()){
progressBar.dismiss();
}
您试图使用“进度”对话框做什么?我使用的是“地图”,所以在按“地图”后需要时间刷新,所以我想使用“进度”对话框。类似于:这种操作:mapView.deleteAllAnnotationsAndCustomPOIs();添加注释(驱动方向destpoint);mapView.centerMapOnPosition(新的SKCoordinate(drivingDirectionDestPoint.GetLength(),drivingDirectionDestPoint.getLatitude())代码>发生。所以,我想克服它。你不必在backpress上实现它。如果每个片段中都有一个异步任务。。在onPreExecute()上启动对话框,并在PostExecute()上关闭它。因此,在后按时在中实现进度对话框不是一个好主意。。因为在backPress上,您实际上不知道需要等待多长时间才能将数据加载到新片段上。最好的方法是在网络调用之前使用onPreExecute中的进度对话框,并在网络调用之后的onPostExecute中取消该对话框。通过这种方式,您还可以显示进度(如加载…90%)。很少有任务像:mapView.deleteAllAnnotationsAndCustomPOIs();添加注释(驱动方向destpoint);mapView.centerMapOnPosition(新的SKCoordinate(drivingDirectionDestPoint.GetLength(),drivingDirectionDestPoint.getLatitude())代码>按视图反向按后进入视图。