2013-03-20 79 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"); 
    }