2013-10-22 263 views
1

我基本上有一個分爲兩半的佈局。在我的左邊,我有一些按鈕可以觸發右側佈局中顯示的各種片段。重用/恢復片段,而不必重新創建它

enter image description here

當我點擊相應的片段以所述片段顯示區域加載每個按鈕。某些片段(例如片段A和片段D)通過查詢數據庫或從互聯網獲取數據等來顯示覆雜數據。只要應用程序正在運行,該數據一旦加載就不會改變。我的問題是我可以將片段恢復到之前的狀態並顯示它。要更清楚 - >我點擊Fragment A按鈕,在Fragment A類中完成所有計算並顯示,然後單擊Fragment B並顯示B片段,然後C。現在,當我點擊Fragment A按鈕時,我只是希望它加載數據,而不是重做計算/連接/數據庫查詢。

代碼中使用了:

public void onClick(View v) { 
    switch (position) { 
      case 0: 
       newContent = new FragmentA(); 
       break; 
      case 1: 
       newContent = new FragmentB(); 
       break; 
      case 2: 
       newContent = new FragmentC(); 
       break; 
      case 3: 
        newContent = new FragmentD(); 
       break; 
       case 4: 
       newContent = new FragmentE(); 
       break; 
         default: break; 
        } 
        if (newContent != null){ 
     mContent = newContent; 
     switchFragment(newContent); 
     } 

} 

public void switchFragment(Fragment fragment) { 
    mContent = fragment; 
    getSupportFragmentManager() 
    .beginTransaction() 
    .replace(R.id.replacelayout, fragment) 
    .commit(); 
} 

片段代碼示例

public class FragmentA extends Fragment { 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragmenta, container, false); 
    } 

@Override 
public void onActivityCreated (Bundle savedInstanceState){ 
super.onActivityCreated(savedInstanceState);    


    RUN ASYNTASKS TO CONNECT/QUERYDB AND DIPLAY THE DATA 

} 
    } 

不知道如何去了解它 - 使用堆棧中? onResume()?因爲我不確定在調用.replace時調用了哪個函數。

由於提前

+0

你找到了一個解決方案? –

回答

0

而不是使用fragment.replace可以使用fragment.show和fragment.hide fragmentByTagName的。這將保持他們在記憶中的狀態。它可能看起來像這樣。

FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     //find the fragment by View or Tag 
      MyFrag myFrag = (MyFrag)fragmentManager.findFragmentByTag(TAG); 
      fragmentTransaction.hide(myOldFrag); 
      fragmentTransaction.show(myNewFrag); 
      fragmentTransaction.commit(); 
0
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      Fragment newContent = null; 
      getSupportFragmentManager() 
      .beginTransaction() 
      .hide(mContent) 
      .commit(); 

      switch (position) { 
      case 0: 
       getSupportFragmentManager() 
       .beginTransaction() 
       .show(home) 
       .commit(); 
       mContent = home; 
       getSlidingMenu().showContent(); 
       break; 
      case 1: 

       if(ifdownexsists == true){ 
        getSupportFragmentManager() 
         .beginTransaction() 
         .show(download) 
         .commit(); 
         mContent = download; 
         getSlidingMenu().showContent();} 
       else 
       { download = new DownloadFile(); 
        getSupportFragmentManager() 
        .beginTransaction() 
        .add(R.id.replacelayout, download) 
        .commit(); 
        mContent = download; 
        ifdownexsists = true; 
        getSlidingMenu().showContent(); 
       } 

       break; 
      case 2: 
       if(ifexsists == true){ 
        getSupportFragmentManager() 
         .beginTransaction() 
         .show(oldCheckValue) 
         .commit(); 
         mContent = oldCheckValue; 
         getSlidingMenu().showContent();} 
       else 
       { oldCheckValue = new CheckValues(); 
        getSupportFragmentManager() 
        .beginTransaction() 
        .add(R.id.replacelayout, oldCheckValue) 
        .commit(); 
        mContent = oldCheckValue; 
        ifexsists = true; 
        getSlidingMenu().showContent(); 
       } 
       break; 

就像一個魅力......即使保持在後臺progressbars的進步和他們正確地顯示了當片段再次變得可見

相關問題