gpt4 book ai didi

android - 如何等到 fragment 被删除

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:09 26 4
gpt4 key购买 nike

我有一个包含动态 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 happenimmediately; it will be scheduled as work on the main thread to bedone 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com