源码:
/**
* Return the {@link Context} this fragment is currently associated with.
*
* @throws IllegalStateException if not currently associated with a context.
* @see #getContext()
*/
@NonNull
public final Context requireContext() {
Context context = getContext();
if (context == null) {
throw new IllegalStateException("Fragment " + this + " not attached to a context.");
}
return context;
}
/**
* Return the {@link Context} this fragment is currently associated with.
*
* @see #requireContext()
*/
@Nullable
public Context getContext() {
return mHost == null ? null : mHost.getContext();
}
原因:在DialogFragment中下载apk安装包的回调中有安装功能,安装apk需要context,从requireContext获取,但是此时DialogFragment已经onDestroy了,失去了与Activity的绑定,因此抛出异常。
解决:传入application作为context安装时所需context
本文探讨了在DialogFragment中因onDestroy导致失去与Activity绑定,从而无法通过requireContext()获取context的问题。提供了使用application作为context的解决方案,确保在任何情况下都能顺利进行apk安装。
1300

被折叠的 条评论
为什么被折叠?



