【问题标题】:Fragments Memory Managment片段内存管理
【发布时间】:2013-10-29 22:11:27
【问题描述】:

我将列表视图放在项目单击侦听器代码上供您参考。在列表视图中单击每个项目时,我将片段替换为另一个片段。我将主片段(片段 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();
                 }     
                   } 
                });

如果我点击 movies,fragment1 会被新的 fragment(Item 1) 替换 如果我点击 serials,fragment1 将被新的 Fragment 替换(第 2 项) 我在想,当我切换到新片段(项目 1)时,片段 1 内存将被释放,当我单击片段(项目 2)时,片段(项目 1)将被释放,告诉我它会在相同的情况下工作方式与否。因为我需要为每个列表项单击使用更多数量的片段(列表中的项目数量会更多)。我想知道它是否会以同样的方式发生是否有任何方法来管理片段的内存。建议我一个更好的方法来做到这一点,因为我希望我的应用程序在没有内存泄漏的情况下运行。

【问题讨论】:

    标签: android memory-management fragment


    【解决方案1】:

    您可以使用片段生命周期方法简单地检查这一点。

    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");
      }
    

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      相关资源
      最近更新 更多