我有一个带有动态片段的活动.我需要在删除一个片段后运行一些代码,但是remove(myFragment).commit()是异步执行的,我不知道该片段何时被删除.这是我的代码: final FragmentTransaction ft = getFra
final FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.remove(myFragment).commit(); //wait until the fragment is removed and then execute rest of my code
从文档:
如果您使用片段的onDetach方法来调用活动并告诉它已完成,该怎么办?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.
class MyFrag extends Fragment { private Activity act; @Override public void onAttach(Activity activity) { act = activity; } @Override public void onDetatch() { act.fragGone(); } }
在活动中:
public fragGone() { //do something, update a boolean, refresh view, etc. }