2013-03-20 80 views
0

我將列表視圖放置在項目單擊監聽器代碼供您參考。每個項目單擊列表視圖中我將用另一個片段替換片段.i將主片段(片段1)放入幀中佈局文件。我只是在相同的視圖中切換片段。片段內存管理

listView.setOnItemClickListener(new OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 

        if(array[position].equalsIgnoreCase("movies")){ 
         FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
         transaction.replace(R.id.fragment1, new next()); 

         transaction.commit(); 
        } 
        if(array[position].equalsIgnoreCase("serials")){ 
         FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
        transaction.replace(R.id.fragment1, new Item2()); 

         transaction.commit(); 
       } 
        if(array[position].equalsIgnoreCase("restaurents")){ 
         FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
        transaction.replace(R.id.fragment1, new Item3()); 

         transaction.commit(); 
       }  
        } 
       }); 

如果我點擊的片段1被新片段(第1項)同樣 更換電影,如果我在連續的片段1被新片段取代(第2項) 我想到點擊當我切換到新的片段(Item 1)時,片段1的內存將被釋放,並且當我點擊片段(Item 2)時,片段(Item 1)將被釋放,告訴我它將以相同的方式工作或不工作。因爲我需要爲每個列表項目單擊使用更多數量的片段(列表中的項目數量會更多)。我想知道它是否會以相同的方式發生,有沒有辦法管理片段的內存。建議我一個更好的方法來做到這一點,因爲我希望我的應用程序能夠運行內存泄漏。

回答

0

您可以使用片段生命週期方法來簡單檢查此問題。

public void onStart() { 
    super.onStart(); 
    Log.d(LOG_TAG, "Fragment1 onStart"); 
    } 

    public void onResume() { 
    super.onResume(); 
    Log.d(LOG_TAG, "Fragment1 onResume"); 
    } 

    public void onPause() { 
    super.onPause(); 
    Log.d(LOG_TAG, "Fragment1 onPause"); 
    } 

    public void onStop() { 
    super.onStop(); 
    Log.d(LOG_TAG, "Fragment1 onStop"); 
    } 

    public void onDestroyView() { 
    super.onDestroyView(); 
    Log.d(LOG_TAG, "Fragment1 onDestroyView"); 
    } 

    public void onDestroy() { 
    super.onDestroy(); 
    Log.d(LOG_TAG, "Fragment1 onDestroy"); 
    } 

    public void onDetach() { 
    super.onDetach(); 
    Log.d(LOG_TAG, "Fragment1 onDetach"); 
    }