0

我有一個按鈕的活動。當按鈕被點擊時,一個新的活動開始,並在這個活動中,各種數據從web.But獲取當我點擊按鈕,第一個活動持有一個而在3-4秒後,第二個活動以就緒數據開始。顯然,在第二個活動可見之前,應用程序嘗試獲取數據。但是,首先,我希望第二個活動對用戶可見,在獲取數據之後必須啓動。 我檢查活動的生命週期和更換取到在onStart(),但不工作Android在活動後做某事

@Override 
protected void onStart() { 
      super.onStart(); 
      //Fetch data 
} 

我怎麼能這樣做?

這裏是我的代碼(只次活動):

public class GuzergahActivity extends android.support.v4.app.FragmentActivity{ 

     private GoogleMap map; 
     private GuzergahOverlay overlay; 
     private WebView webview; 
     private ImageView imageview; 
     private ProgressDialog dialog; 
     private int flag; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     overridePendingTransition(0, 0); 
     setContentView(R.layout.guzergah); 
     flag=this.getIntent().getExtras().getInt("flag"); 
     setUpMapIfNeeded(); 
     setUpView(); 
     } 

     @Override 
     protected void onResume() { 
      super.onResume(); 
      setUpMapIfNeeded(); 
     } 

     @Override 
     protected void onDestroy() { 
     if (dialog!=null) { 
      dialog.dismiss(); 
     } 
     super.onDestroy(); 
     } 

     private void setUpMapIfNeeded() { 
      // Do a null check to confirm that we have not already instantiated the map. 
      if (map == null) { 
       map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.guzergah_fragment)).getMap(); 
       if(map!=null){ 
        setUpMap(); 
       } 
      } 
     } 

    private void setUpMap(){ 

     try { 
      String result=new MyNewTask().execute().get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     overlay.setOverlay(map); 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.910526,32.804435),15)); 
     map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
    } 


    private void setUpView() { 
      //Binding imageview,webview...and setting up their features 
    } 

    private class MyNewTask extends AsyncTask<String,Integer,String> 
    { 
     @Override 
     protected void onPreExecute() 
     { 

      dialog= new ProgressDialog(GuzergahActivity.this); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(false); 
      dialog.setMessage("Downloading! Please wait...!"); 
      dialog.show(); 

     } 


     @Override 
     protected String doInBackground(String... params) 
     { 
      /* Fetching data is in this class - GuzergahOverlay*/ 
      overlay = new GuzergahOverlay(flag); 

      return "Done"; 

     } 

     @Override 
     protected void onPostExecute(String result) 
     { 
      super.onPostExecute(result); 
      Log.i("result","" +result); 
      if(result!=null){ 
       dialog.dismiss(); 
      } 
     } 

    } //end MyNewTask 
} 



public class GuzergahOverlay { 

private MarkerOptions beytepe_kampusu; 
private MarkerOptions sihhiye_kampusu; 
private ArrayList<PolylineOptions> lines; 
private ArrayList<MarkerOptions> stops; 

public GuzergahOverlay(int mode) { 
    beytepe_kampusu=new MarkerOptions().position(new LatLng(39.87159,32.735435)).title("Hacettepe Üniversitesi Beytepe Yerleşkesi").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_beytepe)); 
    sihhiye_kampusu=new MarkerOptions().position(new LatLng(39.931599,32.862365)).title("Hacettepe Üniversitesi Sıhhıye Yerleşkesi").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_sihhiye)); 
    getLinesAndStops(mode); //Here is fetching geopoint and preparing lines and stops arraylists 
} 

private void getLinesAndStops(int mode) { 

    // Fetching data from Parse.com 
      // Fillinp up lines and stops arraylists 
} 




public void setOverlay(GoogleMap map){ 
    map.addMarker(beytepe_kampusu); 
    map.addMarker(sihhiye_kampusu); 
    for(PolylineOptions polyline : lines){ 
     map.addPolyline(polyline); 
    } 
    for(MarkerOptions marker : stops){ 
     map.addMarker(marker); 
    } 
} 
} 

有了上面的代碼,當我點擊按鈕,第一個活動持有了一段時間,獲取數據finished.Progress對話框時,第二項活動是可見的僅在幾個毫秒內可見

+0

如果我成功了,也許我的progressdialog在第二項活動中出現 –

回答

1

聽起來就像在UI線程中運行所有代碼一樣。用更多的代碼來提供更好的示例會更好。

在你的第二個活動:

private class BackgroundTask extends AsyncTask<Void, Void, Void> { 

    // Before running code in separate thread 
    @Override 
    protected void onPreExecute() { 
     // setup loading dialog 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // do long running code 
     return null; 
    } 

    // after executing the code in the thread 
    @Override 
    protected void onPostExecute(Void arg) { 
     // dismiss your dialog 

    } 

} 

而且裏面的onCreate:

new BackgroundTask().execute(); 

編輯:

現在,我已經看到了你的代碼,我會建議你嘗試是這樣的:

private void setUpMap(){ 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(new  LatLng(39.910526,32.804435),15)); 
     map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
     new Handler().postDelayed(new Runnable() { 
      public void run() { 
       try { 
        String result = new MyNewTask().execute(); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       /* 
       * you should move this call to onPostExecute 
       * then you will not need to call execute().get() 
       * which takes away the advantage of AsyncTask 
       * as it then is run on UI thread 
       */ 

       overlay.setOverlay(map); 
      } 
     }, 2000); 

    } 

設置地圖是一項用戶界面繁重的任務,因此根據我的經驗,在加載標記之前等待一會兒看起來會更好。此外,這應該使對話框在被解散之前正確顯示。

+0

我editted我的問題,我想我不能分開UI和異步任務... –

+0

真正的所有調用添加標記映射之類必須在UI線程,可以在onPostExcecute但認爲你應該移動超.onPostExecute(result);到方法底部 – cYrixmorten

+0

我已更新我的答案,以便您希望得到您正在查找的行爲 – cYrixmorten

1

您應該將數據提取方法移動到AsyncTask。你不應該從UI線程的遠程源獲取數據。如果以這種方式將該操作移動到後臺線程中,則UI將具有更高的優先級,並且應該可以快速加載,同時仍然允許完成異步任務。你甚至可以在o​​nCreate或onStart中啓動任務,你應該看到更好的性能。

+0

感謝您的回覆,我用代碼編輯了我的問題。可以再看一次嗎? –

+0

@SedatKURT你的編輯看起來像你正朝着正確的方向前進。現在有什麼問題? – mattgmg1990

+0

我在問題的結尾說:「用上面的代碼,當我點擊按鈕時,第一個活動暫時擱置一會兒,第二個活動在讀取完數據時可見。進度對話框只有幾個毫秒可見」。這是我的問題:( –

相關問題