Closed flobizDipen closed 3 years ago
thx for the report.
But I'm afraid you should avoid calling functionWithPermissionCheck
when activity could be null because then we can't do anything(PermissionChecker.checkSelfPermission
requires context and we eventually need activity or context).
please use "by lazy" in your fragment private val showCamera by lazy { constructPermissionsRequest( permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE ), onShowRationale = ::onCameraShowRationale, onPermissionDenied = ::onCameraDenied, onNeverAskAgain = ::onCameraNeverAskAgain ) { startActivity(Intent(mActivity, XXXX::class.java)) } }
Description
Fatal Exception: java.lang.IllegalStateException: Fragment not attached to an activity. at androidx.fragment.app.Fragment.requireActivity(Fragment.java:833) at com.packageName.FragmentPermissionsDispatcher.functionWithPermissionCheck(FragmentPermissionsDispatcher.java:33)
This is happening because
hasSelfPermissions
requires activity context and for that, you guys are usingrequireActivity()
which can be null too@NonNull public final FragmentActivity requireActivity() { FragmentActivity activity = getActivity(); if (activity == null) { throw new IllegalStateException("Fragment " + this + " not attached to an activity."); } return activity; }
Environment