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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章