2015-02-23 111 views
0

我正在應用程序中有一個SearchActivity;搜索活動只是「SearchFragment」的一個容器,而「SearchFragment」在ActionBar中包含SearchView,其主要佈局的RecyclerView用於顯示搜索結果。假設用戶執行搜索,結果顯示在RecyclerView內,然後用戶旋轉屏幕......在這種情況下,我使用setRetainInstance(true)方法。我認爲我的自定義ArrayAdapter數據將被保存,但事實並非如此。我知道我可以保存ArrayAdapter數據BundleonSaveInstanceState()方法。但我想知道爲什麼setRetainInstance()方法不起作用。setRetainInstance()方法是否保留ArrayAdapter數據?

public class SearchFragment extends Fragment 
       implements SearchView.OnQueryTextListener, 
       ResultsListAdapter.ItemClickListener { 

      private ResultsListAdapter mListAdapter; 
      private CustomRecyclerView resultsList; 

      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setHasOptionsMenu(true); 
      } 

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

       View rootView = inflater.inflate(R.layout.fragment_search, container, false); 

       RecyclerView.LayoutManager linearManager = new LinearLayoutManager(getActivity()); 
       mListAdapter = new ResultsListAdapter(new ArrayList<Kiosk>(), this); 

       resultsList.setAdapter(mListAdapter); 
       resultsList.setLayoutManager(linearManager); 
       resultsList.setSetEmptyView(rootView.findViewById(R.id.empty_view)); 

       if (savedInstanceState == null){ 
        setRetainInstance(true); 
       } 

       return rootView; 
      } 
     @Override 
      public boolean onQueryTextSubmit(String s) { 
       if(s.isEmpty()) { 
        //Set an empty array, to set an empty view 
        mListAdapter.changeData(new ArrayList<Kiosk>()); 
        mListAdapter.notifyDataSetChanged(); 
       } 

       ArrayList<Kiosk> kiosksFound = performSearch(s); 

       mListAdapter.changeData(kiosksFound); 
       mListAdapter.notifyDataSetChanged(); 

       return false; 
      } 

      @Override 
      public boolean onQueryTextChange(String s) { 
       if(s.isEmpty()) { 
        //Set an empty array, to set an empty view 
        mListAdapter.changeData(new ArrayList<Kiosk>()); 
        mListAdapter.notifyDataSetChanged(); 
        return false; 
       } 

       ArrayList<Kiosk> kiosksFound = performSearch(s); 

       mListAdapter.changeData(kiosksFound); 
       mListAdapter.notifyDataSetChanged(); 

       return false; 
      } 

    } 
+0

我想知道是否在'Fragment'的生命週期早期調用方法會產生影響。你有沒有嘗試在'onCreate()'中調用'setRetainInstance(true)'? – Emmanuel 2015-02-23 05:20:28

+0

嗨@Emmanuel,是的,我嘗試過,但結果是一樣的。 – Silmood 2015-02-23 06:01:36

回答

1

即使保留狀態,onCreateView仍將被調用。由於您在那裏重新創建列表視圖,數據將被清除。

+0

是啊!謝謝!。我在'onCreate()'方法中初始化適配器,而不是'onCreateView()'。這樣數據不會被清除。 – Silmood 2015-02-23 22:58:04

+1

@Silmood這不是一個好方法,因爲適配器持有對視圖的引用,實際上,你應該在'onDestroyView()'內部調用'adapter = null'來避免泄漏。我建議將你的數據保存在fragment類的一個字段中,並在'onCreateView()'中重新創建它後將它添加到適配器中' – 2016-09-24 21:51:45

0

如果您將視圖和模型分開,那麼您不應該將數據保留在視圖內,例如活動,片段或適配器,那麼當視圖破壞時您不會擔心數據丟失,當您的視圖模型在不同視圖中具有多種表示形式,例如將其顯示爲列表視圖或網格視圖。 Model–view–controller

相關問題