gpt4 book ai didi

android - Activity.isFinishing() 的 fragment 等价物是什么?

转载 作者:IT老高 更新时间:2023-10-28 23:33:23 35 4
gpt4 key购买 nike

在我的 Activity 中,我经常使用这个成语:

@Override
public void onDestroy() {
super.onDestroy();
if (isFinishing() != true) return;
// do some final cleanup since we're going away for good
}

Fragment 有一个 onDestroy() 方法,但是 isFinishing() 的等价物是什么?我应该从 fragment 的 onDestroy() 中检查 getActivity().isFinishing() 吗?

编辑添加:

这是我在各种情况下得到的回调(按顺序),以及 getActivity() 是否返回 null 或非 null,如果非 null,则返回 getActivity 的值().isFinishing():

场景 #1:DialogFragment 正在显示并且用户点击返回按钮(即需要释放引用):

onDismiss(): activity = non-null, finishing = false
onDestroy(): activity = non-null, finishing = false

场景 #2:DialogFragment 正在显示并且父 Activity 完成(即需要释放引用):

onDestroy(): activity = non-null, finishing = true
onDismiss(): activity = null, finishing = n/a

场景 #3:DialogFragment 正在显示,操作系统暂时破坏父 Activity (即不应释放引用):

onDestroy(): activity = non-null, finishing = false
onDismiss(): activity = null, finishing = n/a

最佳答案

fragment 有一个名为 isRemoving() 的方法当从 Activity 中移除 fragment 时,这是真的:

Return true if this fragment is currently being removed from its activity. This is not whether its activity is finishing, but rather whether it is in the process of being removed from its activity.

关于android - Activity.isFinishing() 的 fragment 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25450763/

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