看这个 "startActivityForResult()" 代码,这个函数被弃用了。如何在 Fragment 中更改此代码?
Look this "startActivityForResult()" codes, this function was deprecated. How to change this codes in Fragment?
看这个开始ActivityForResult() 代码,这个函数被弃用了。
如何在 Fragment 中更改此代码?
“从包含 Activity 的片段调用 Activity.startActivityForResult(Intent, int)”是什么意思??
拜托,简单指导我...
private const val REQUEST_PHOTO = 2
class ExampleFragment:Fragment(), DatePickerFragment.Callbacks {
private lateinit var photoUri : Uri
override fun onStart() {
super.onStart()
...
photoButton.apply {
val packageManager : PackageManager = requireActivity().packageManager
val captureImage = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val resolvedActivity : ResolveInfo? =
packageManager.resolveActivity(captureImage,
PackageManager.MATCH_DEFAULT_ONLY)
if (resolvedActivity == null) {
isEnabled = false
}
setOnClickListener {
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
val cameraActivities : List<ResolveInfo> =
packageManager.queryIntentActivities(captureImage, PackageManager.MATCH_DEFAULT_ONLY)
for (cameraActivity in cameraActivities) {
requireActivity().grantUriPermission(
cameraActivity.activityInfo.packageName,photoUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
startActivityForResult(captureImage, REQUEST_PHOTO) // <<<< Deprecated
}
}
}
首先创建一个名为captureImageLauncher
的ActivityResultLauncher
类型的变量
你必须像这样注册一个结果回调:(这是以前使用的 onActivityResult
方法的替代方法)。因此,您可以在此回调
中将 onActivityResult
下的所有代码移至此处
private val captureImageLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result: ActivityResult ->
// Here You handle the result that the activity sent back
// You can use methods like result.resultCode or result.data to retrieve information
}
这将创建一个 ActivityResultLauncher
,您稍后将使用它来代替 startActivityForResult
,就像这样:(而不是 startActivityForResult
方法。您也不需要明确的请求代码,因为它使事情变得不必要的复杂)
captureImageLauncher.launch(captureImage) // captureImage is the intent that You've created
更多信息here。
看这个开始ActivityForResult() 代码,这个函数被弃用了。 如何在 Fragment 中更改此代码?
“从包含 Activity 的片段调用 Activity.startActivityForResult(Intent, int)”是什么意思??
拜托,简单指导我...
private const val REQUEST_PHOTO = 2
class ExampleFragment:Fragment(), DatePickerFragment.Callbacks {
private lateinit var photoUri : Uri
override fun onStart() {
super.onStart()
...
photoButton.apply {
val packageManager : PackageManager = requireActivity().packageManager
val captureImage = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val resolvedActivity : ResolveInfo? =
packageManager.resolveActivity(captureImage,
PackageManager.MATCH_DEFAULT_ONLY)
if (resolvedActivity == null) {
isEnabled = false
}
setOnClickListener {
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
val cameraActivities : List<ResolveInfo> =
packageManager.queryIntentActivities(captureImage, PackageManager.MATCH_DEFAULT_ONLY)
for (cameraActivity in cameraActivities) {
requireActivity().grantUriPermission(
cameraActivity.activityInfo.packageName,photoUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
startActivityForResult(captureImage, REQUEST_PHOTO) // <<<< Deprecated
}
}
}
首先创建一个名为captureImageLauncher
ActivityResultLauncher
类型的变量
你必须像这样注册一个结果回调:(这是以前使用的 onActivityResult
方法的替代方法)。因此,您可以在此回调
onActivityResult
下的所有代码移至此处
private val captureImageLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result: ActivityResult ->
// Here You handle the result that the activity sent back
// You can use methods like result.resultCode or result.data to retrieve information
}
这将创建一个 ActivityResultLauncher
,您稍后将使用它来代替 startActivityForResult
,就像这样:(而不是 startActivityForResult
方法。您也不需要明确的请求代码,因为它使事情变得不必要的复杂)
captureImageLauncher.launch(captureImage) // captureImage is the intent that You've created
更多信息here。