2017-01-03 100 views
0

我要麼後navigation startroute shown後按Back Button顯示ProgressDialog後返回按鈕按下

我的工作FragmentView按順序排列如下: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); 

但是,我在哪裏可以在這裏使用的dismiss()方法返回的畫面時back press後?可以做些什麼來解決這個問題?

+0

什麼是你想使用進度對話框? –

+0

我使用地圖,所以後面按下地圖需要時間來刷新,所以我想使用進度對話框。像這樣的操作:'mapView.deleteAllAnnotationsAndCustomPOIs(); addAnnotation(drivingDirectionDestPoint); mapView.centerMapOnPosition(新SKCoordinate(drivingDirectionDestPoint.getLongitude(),drivingDirectionDestPoint.getLatitude())); '發生。所以,我想克服它。 –

+0

你不必實現onBackPress。如果每個片段中都有一個AsyncTask,請在onPreExecute()上啓動對話框,然後在PostExecute()上關閉它。 –

回答

0

你想展示你的ProgressDialog一些任務(需要一些時間才能完成)開始時,當任務完成後關閉它。

我看不到這樣的任務在您的代碼段,但如果有一個我認爲它提供了一個API來註冊一個監聽器中,你應該表現出和解散你的ProgressDialog。

如果沒有,需要一些時間來完成任務,這是不好的做法,以顯示ProgressDialog。嘗試一種不同的方法。也許一些動畫(例如淡出視圖,更改數據,淡入視圖)。

+0

有幾個任務,如:'mapView.deleteAllAnnotationsAndCustomPOIs(); addAnnotation(drivingDirectionDestPoint); mapView.centerMapOnPosition(新SKCoordinate(drivingDirectionDestPoint.getLongitude(),drivingDirectionDestPoint.getLatitude()));在後面按下視圖後按[ ]。 –

+0

你的建議也是一個好主意或一個選項。 –

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

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