0

我将列表视图放在项目单击侦听器代码上供您参考。在列表视图中单击每个项目时,我将用另一个片段替换片段。我将主要片段(片段 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)替换,我认为当我切换到新片段时片段 1 内存将被释放(第 1 项)和片段(第 1 项)将在我单击片段(第 2 项)时被释放,告诉我它是否会以相同的方式工作。因为我需要为每个列表项单击使用更多数量的片段(列表中的项目数会更多)。我想知道它是否会以同样的方式发生是否有任何方法来管理片段的内存。建议我一个更好的方法来做到这一点,因为我希望我的应用程序在没有内存泄漏的情况下运行。

4

1 回答 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");
  }
于 2013-10-29T14:11:27.247 回答