1>嘗試在堆棧保持片段(添加和彈出而要添加新片段)
例如: 私人堆棧fragmentStack =新堆棧();
//代碼在堆棧中添加新的片段,在前面的堆棧中添加前一個片段 FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment mFragment = new Fragment(); // you can replace this with your fragment
ft.add(R.id.container, mFragment); // you can also add fragment id so that if you can reuse that fragment if it is memory.
fragmentStack.lastElement().onPause();
ft.hide(fragmentStack.lastElement());
fragmentStack.push(resultListFragment);
ft.commit();
//代碼從頂部除去可見片段並恢復回棧片段
如果(fragmentStack.size()== 2)//用於檢查是否其是任何片段在堆棧是肯定比onbackbutton事件返回事件{FragmentTransaction ft = fragmentManager.beginTransaction(); fragmentStack.lastElement()。onPause(); ft.remove(fragmentStack.pop()); fragmentStack.lastElement()。onResume(); ft.show(fragmentStack.lastElement()); ft.commit(); }
2>如果以上操作不解決您OutOfMemory錯誤比片段生命週期的onCreate(捆綁)系統回調方法使用setRetaininstance(真)的問題,所以如果他們已經在內存中它不會產生新的片段。
你應該檢查是否有任何強烈的引用FragmentB沒有被銷燬時釋放。如果可能的話,在這裏分享你的代碼片段,這樣每個人都可以給你建議。 – Robo
抱歉,我無法分享我的代碼。我在這裏創建了像我的流程應用程序這樣的示例項目:https://github.com/sinhpn92/FragmentStackTest。 我的樣品沒有這個問題。但在我的項目中,我有上面的問題。我不知道爲什麼?這個問題有什麼原因嗎? –