2012-11-13 18 views
0

我有一個確定位置的主要活動。在這個主要活動中,我首先添加一個splashscreen片段。目前我發現第一個位置/ GPS工作正常並找到了一個位置,我想用我的主菜單替換這個splashscreen。 相關的代碼在MainActivity:當前位置的切換片段被發現

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.frame); 

    //Check that the activity is using the layout version 
    // the framelayout 

    if(findViewById(R.id.fragment_container)!=null){ 
     //To avoid overlapping fragments check if we are restored from a state, then we don't have to do anything 
     if(savedInstanceState != null){ 
      return; 
     } 

     Splash splashscr = new Splash(); 
     Main main = new Main(); 

     getSupportFragmentManager().beginTransaction() 
     .add(R.id.fragment_container, splashscr).commit(); 

    // I **think something should be added here 
//to check if a location has been found yet.** 

     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.fragment_container, main); 
     transaction.commit(); 
     } 


} 

我嘗試添加在該位置一段時間(foundlocation =真!)。然後,在onLocationChanged()或getLocation()的末尾將找到的位置設置爲true。但是,它並沒有解決我的問題。有人可以幫我嗎?如果我沒有把我的問題弄清楚,請說清楚。

編輯:考慮加布的評論後,我想這(locationfound的啓動,活動類假公佈爾)。但是,如果我現在使用DDMS發送位置,那麼splashscreen將不會消失。如果我是正確的,第一次改變位置時,布爾值仍然是錯誤的,因此它應該切換片段。 我試過沒有if語句,然後它工作。我在這裏忘了什麼?

public void onLocationChanged(final Location location) { 

     if(locationfound=false) 
     { 
//Some irrelevant code about saving the new location 

     Main main = new Main(); 

     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.fragment_container, main); 
     transaction.commit(); 

     locationfound=true; 
      } 

//changing value of sometextboxes in the Main fragment 

    } 

回答

0

不要試圖在onCreate中等待,以阻止UI線程導致無響應的UI。相反,您應該在第一次調用onLocationChanged函數時通過切換片段(通過使用布爾標誌變量來判斷它是否是第一次)。

+0

謝謝,這的確聽起來更好一些。查看我編輯的新問題。顯然我正在用我的布爾標誌做一些錯誤......:s – Siddler

+0

修正了它。顯然它正在使用一個計數器並檢查它是否小於1.儘管沒有看到很大的區別.. – Siddler