2015-06-21 70 views
2

我在我的Android應用程序中有4個片段。我有一個FragmentPagerAdapter來填充ViewPager片段內存泄漏

另外我在主要活動中擴展FragmentActivity。

問題是當我在片段中滑動,其他片段正在銷燬(垃圾回收器?)。我怎樣才能避免這種情況?

FragmentAdapter:

package com.quiz.Adapter; 

public class FragmentAdapter extends FragmentPagerAdapter { 

    private List<Fragment> fragments = new ArrayList<>(); 
    private Context ctx; 

    public FragmentAdapter(FragmentManager fm, Context ctx) { 
     super(fm); 
     this.ctx = ctx; 
     fragments.add(new FragmentMenu()); //same 
     fragments.add(new FragmentMain()); 
     fragments.add(new FragmentMenu()); //same 
     fragments.add(new FragmentMenu()); //same 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return fragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     return fragments.size(); 
    } 


} 

3片段具有一個TextView。另一個片段上有一個按鈕和一個進度條:

package com.quiz.Activity.Fragments; 


public class FragmentMain extends Fragment implements View.OnClickListener { 

    Timer timer; 
    int gameStartIn = 0, ONE = 1, INTERVAL = 1000; 
    Context context; 
    boolean joinedToMulti = false; 
    NormalButton multiGameButton; 
    NextGameCounter nextGameCounter; //progressbar 
    Store store; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_main, container, false); 

     context = this.getActivity().getApplicationContext(); 
     store = new Store(context); 
     multiGameButton = (NormalButton)view.findViewById(R.id.multiGameButton); 
     multiGameButton.setOnClickListener(this); 
     nextGameCounter = (NextGameCounter)view.findViewById(R.id.nextGameCounter); 

     init(); 

     Toast("onCreateView"); 


     return view; 
    } 

    void Toast(String e){ 
     Toast.makeText(context, "_"+e, Toast.LENGTH_SHORT).show(); 
    } 

    private void init() { 

     getNextRoundDate(); 

     timer = new Timer(); 
     timer.scheduleAtFixedRate(new TimerTask() { 

      @Override 
      public void run() { 

       gameStartIn--; 

       if(gameStartIn < ONE) { 
        gameStartIn = 1; 
        timer = new Timer(); 
        getNextRoundDate(); 
       } else { 
        //update ui 
        getActivity().runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          nextGameCounter.setProgress(gameStartIn); 
         } 
        }); 
       } 

      } 
     }, 0, INTERVAL); 
    } 

    private void getNextRoundDate() { 
     new AsyncTask<Void, Void, ResponseModel>() { 
      @Override 
      protected ResponseModel doInBackground(Void... voids) { 
       ResponseModel responseModel = ApiManager.getService().GameGetNextRoundDate(); 
       return responseModel; 
      } 

      @Override 
      protected void onPostExecute(ResponseModel e){ 
       gameStartIn = Integer.valueOf(e.getInfo()); //seconds 
      } 
     }.execute(); 
    } 

    @Override 
    public void onClick(View view) { 

     if(view.getId() == R.id.multiGameButton){ 
      if(joinedToMulti) 
       return; 

      joinedToMulti = true; 
      requestNewGame(); 
     } 
    } 

    @Override 
    public void onPause(){ 
     super.onPause(); 
     stopGame(); 
    } 

    @Override 
    public void onResume(){ 
     super.onResume(); 
     multiGameButton.setText(getResources().getString(R.string.main_multiplayer_button_text)); 
    } 

    private void stopGame() { 
     if(joinedToMulti) 
      sendLeaveLobbyRequest(); 
    } 

    void sendLeaveLobbyRequest(){ 
     new AsyncTask<Void, Void, Void>() { 
      @Override 
      protected Void doInBackground(Void... voids) { 
       ApiManager.getService().GameMatchLeaveRequest(new GameMultiRequestModel(store.getUid())); 
       return null; 
      } 
     }.execute(); 
    } 

    void requestNewGame(){ 
     new AsyncTask<Void, Void, ResponseModel>() { 

      @Override 
      protected ResponseModel doInBackground(Void... voids) { 

       return ApiManager.getService().GameMatchRequest(new GameMultiRequestModel(store.getUid())); 
      } 

      @Override 
      protected void onPostExecute(ResponseModel matchResponseModel){ 
       multiGameButton.setText(getString(R.string.main_multiplayer_joined_button_text)); 
       Toast.makeText(context, "Game starting in "+gameStartIn+" seconds.", Toast.LENGTH_LONG).show(); 
      } 

     }.execute(); 
    } 

} 

回答

2

您可以使用ViewPagersetOffscreenPageLimit (int limit)方法來增加保存在內存碎片的數量。

默認值爲1表示內存中有3個片段。 (活動的1個左側和1個右側)。

你可以嘗試2或3.但不要太高它可能會吃太多的記憶。

+1

我確實將限制設置爲2,現在工作沒有問題。謝謝你的回答。 – Lazy