我有一个包含动态 fragment 的 Activity 。我需要运行一些代码 after 一个 fragment 被删除但是 remove(myFragment).commit()
是异步执行的,我不知道 fragment 何时被删除。这是我的代码:
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.remove(myFragment).commit();
//wait until the fragment is removed and then execute rest of my code
来自文档:
public abstract int commit ()
Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.
最佳答案
如果您使用 fragment 的 onDetach 方法调用 Activity 并告诉它已完成,会怎样?
class MyFrag extends Fragment {
private Activity act;
@Override
public void onAttach(Activity activity) {
act = activity;
}
@Override
public void onDetatch() {
act.fragGone();
}
}
在 Activity 中:
public void fragGone() {
//do something, update a boolean, refresh view, etc.
}
关于android - 如何等到 fragment 被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16842774/