2012-11-09 27 views
0

我正在嘗試創建帶有片段的平板電腦應用程序。屏幕左側有四個按鈕,屏幕的右側部分將根據點擊按鈕而改變。 我已經創建了主要活動和四個片段。每個片段都有其自己的佈局以及多個TextView字段。當應用程序啓動時,它將所有片段加載到RAM中 - 這樣它可以保持片段狀態,以便當用戶從一個片段切換到另一個片段時,所有文本字段都保留其文本值,直到他單擊最後的提交按鈕。該應用程序基於SDK 4.1。該應用程序有點慢,特別是它啓動時。我想知道它是否設計得當,是否有改進它的方法?帶有幾個片段的Android應用程序設計

下面是主要的活動類:

import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    private Button buttonOne; 
    private Button buttonTwo; 
    private Button buttonThree; 
    private Button buttonFour; 

    private Fragment fragmentOne; 
    private Fragment fragmentTwo; 
    private Fragment fragmentThree; 
    private Fragment fragmentFour; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     buttonOne = (Button) findViewById(R.id.button_one); 
     buttonTwo = (Button) findViewById(R.id.button_two); 
     buttonThree = (Button) findViewById(R.id.button_three); 
     buttonFour = (Button) findViewById(R.id.button_four); 

     fragmentOne = new FragmentOne(); 
     fragmentTwo = new FragmentTwo(); 
     fragmentThree = new FragmentThree(); 
     fragmentFour = new FragmentFour(); 

     FragmentTransaction fragmentTransaction = getFragmentManager() 
       .beginTransaction(); 

     fragmentTransaction.add(R.id.frameLayout_one, fragmentOne); 
     fragmentTransaction.add(R.id.frameLayout_one, fragmentTwo); 
     fragmentTransaction.add(R.id.frameLayout_one, fragmentThree); 
     fragmentTransaction.add(R.id.frameLayout_one, fragmentFour); 

     fragmentTransaction.commit(); 

     buttonOne.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       FragmentTransaction fragmentTransaction = getFragmentManager() 
         .beginTransaction(); 
       fragmentTransaction.show(fragmentOne); 
       fragmentTransaction.hide(fragmentTwo); 
       fragmentTransaction.hide(fragmentThree); 
       fragmentTransaction.hide(fragmentFour); 

       fragmentTransaction.commit(); 

      } 
     }); 

     buttonTwo.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       FragmentTransaction fragmentTransaction = getFragmentManager() 
         .beginTransaction(); 
       fragmentTransaction.hide(fragmentOne); 
       fragmentTransaction.show(fragmentTwo); 
       fragmentTransaction.hide(fragmentThree); 
       fragmentTransaction.hide(fragmentFour); 

       fragmentTransaction.commit(); 

      } 
     }); 
     buttonThree.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       FragmentTransaction fragmentTransaction = getFragmentManager() 
         .beginTransaction(); 
       fragmentTransaction.hide(fragmentOne); 
       fragmentTransaction.hide(fragmentTwo); 
       fragmentTransaction.show(fragmentThree); 
       fragmentTransaction.hide(fragmentFour); 

       fragmentTransaction.commit(); 

      } 
     }); 
     buttonFour.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       FragmentTransaction fragmentTransaction = getFragmentManager() 
         .beginTransaction(); 
       fragmentTransaction.hide(fragmentOne); 
       fragmentTransaction.hide(fragmentTwo); 
       fragmentTransaction.hide(fragmentThree); 
       fragmentTransaction.show(fragmentFour); 

       fragmentTransaction.commit(); 

      } 
     }); 

    } 

回答

3

考慮使用FragmentStateViewPager。

在一個理想的世界裏,如果你有幾個碎片在你的情況之間切換,你可能不想把它們留在RAM中。 2或3個片段可能沒問題,4個或更多片段開始推動它。

的FragmentStateViewPager會自動分離並保存你的碎片的狀態,因爲他們動「關閉屏幕」

您將必須實現的onSaveInstanceState在片段中的每一個保存任何非UI成員變量的任何狀態(UI視圖自動保存它們的狀態)。然後在onCreate或onCreateView中恢復它們。當然,如果你的Fragments的onCreate/onCreateViews速度很慢,那麼這對你並沒有多大幫助。首先確保您的Fragments的創作速度很快...首先查找可以從onCreate/onCreate視圖移至onResume的代碼。

+0

謝謝JoshE。我會去嘗試一下。 –