如何等待直到碎片被移除

Meh*_*nai 4 android android-fragments fragmenttransaction

我有一个带有动态片段的活动。我需要删除片段运行一些代码但该代码remove(myFragment).commit()是异步执行的,我不知道何时确切删除了片段。这是我的代码:

final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.remove(myFragment).commit();
//wait until the fragment is removed and then execute rest of my code
Run Code Online (Sandbox Code Playgroud)

从文档中:

公共抽象int commit()

安排此事务的提交。提交不会立即发生;它将在下次线程准备好时安排为主线程上的工作进行安排。

nex*_*006 5

如果您使用片段的onDetach方法调用活动并将其告知完成,该怎么办?

class MyFrag extends Fragment {

    private Activity act;

    @Override
    public void onAttach(Activity activity) {
        act = activity;
    }

    @Override
    public void onDetatch() {
        act.fragGone();
    }
}
Run Code Online (Sandbox Code Playgroud)

并在活动中:

public void fragGone() {
    //do something, update a boolean, refresh view, etc.
}
Run Code Online (Sandbox Code Playgroud)