How to get a Fragment to remove itself, i.e. its equivalent of finish()?

問題:

I'm converting an app to use fragments using the compatibility library.我正在使用兼容性庫將應用程序轉換爲使用片段。 Now currently I have a number of activities (ABCD) which chain onto one another, D has a button 'OK' which when pressed calls finish which then bubbles up through onActivityResult() to additionally destroy C and B.現在我有許多活動 (ABCD) 相互鏈接,D 有一個按鈕“確定”,按下時調用完成,然後通過onActivityResult()冒泡以額外銷燬 C 和 B。

For my pre Honycomb fragment version each activity is effectively a wrapper on fragments Af Bf Cf Df.對於我的前 Honycomb 片段版本,每個活動實際上是片段 Af Bf Cf Df 的包裝器。 All activities are launched via startActivityForResult() and onActivityResult() within each of the fragments can happily call getActivity().finish()所有活動都通過startActivityForResult()onActivityResult() ,每個片段內都可以愉快地調用getActivity().finish()

The problem that I am having though is in my Honeycomb version I only have one activity, A, and fragments Bf, Cf, Df are loaded using the FragmentManager .我遇到的問題是在我的 Honeycomb 版本中,我只有一個活動 A,並且片段 Bf、Cf、Df 是使用FragmentManager加載的。

What I don't understand is what to do in Df when 'OK' is pressed in order to remove fragments Df, Cf, and Bf?我不明白的是當按下“確定”以刪除片段 Df、Cf 和 Bf 時,在 Df 中要做什麼?

I tried having the fragment popping itself off the stack but this resulted in an exception.我嘗試讓片段從堆棧中彈出,但這導致了異常。 onActivityResult() is useless because I have not loaded up the fragment using startActivityForResult() . onActivityResult()沒用,因爲我沒有使用startActivityForResult()加載片段。

Am I thinking about this completely the wrong way?我是否完全以錯誤的方式思考這個問題? Should I be implementing some sort of listener that communicates with either the parent fragment or activity in order to do the pop using the transaction manager?我是否應該實現某種與父片段或活動通信的偵聽器,以便使用事務管理器進行彈出?


解決方案:

參考一: https://en.stackoom.com/question/OlCE
參考二: https://stackoom.com/question/OlCE
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章