Using registerForActivityResult

This commit is contained in:
Prathamesh More 2022-04-21 19:24:04 +05:30
parent 5216a11170
commit 4b4232ce6b
2 changed files with 12 additions and 30 deletions

View file

@ -10,6 +10,7 @@ import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import androidx.activity.addCallback import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.text.parseAsHtml import androidx.core.text.parseAsHtml
@ -273,10 +274,7 @@ abstract class AbsArtistDetailsFragment : AbsMainActivityFragment(R.layout.fragm
R.id.action_set_artist_image -> { R.id.action_set_artist_image -> {
val intent = Intent(Intent.ACTION_GET_CONTENT) val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*" intent.type = "image/*"
startActivityForResult( selectImageLauncher.launch(Intent.createChooser(intent, getString(R.string.pick_from_local_storage)))
Intent.createChooser(intent, getString(R.string.pick_from_local_storage)),
REQUEST_CODE_SELECT_IMAGE
)
return true return true
} }
R.id.action_reset_artist_image -> { R.id.action_reset_artist_image -> {
@ -337,18 +335,11 @@ abstract class AbsArtistDetailsFragment : AbsMainActivityFragment(R.layout.fragm
} }
} }
private val selectImageLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {result->
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (result.resultCode == Activity.RESULT_OK) {
super.onActivityResult(requestCode, resultCode, data) result.data?.data?.let {
when (requestCode) { CustomArtistImageUtil.getInstance(requireContext())
REQUEST_CODE_SELECT_IMAGE -> if (resultCode == Activity.RESULT_OK) { .setCustomArtistImage(artist, it)
data?.data?.let {
CustomArtistImageUtil.getInstance(requireContext())
.setCustomArtistImage(artist, it)
}
}
else -> if (resultCode == Activity.RESULT_OK) {
println("OK")
} }
} }
} }
@ -395,8 +386,4 @@ abstract class AbsArtistDetailsFragment : AbsMainActivityFragment(R.layout.fragm
super.onDestroyView() super.onDestroyView()
_binding = null _binding = null
} }
companion object {
const val REQUEST_CODE_SELECT_IMAGE = 9002
}
} }

View file

@ -22,6 +22,7 @@ import android.os.Bundle
import android.speech.RecognizerIntent import android.speech.RecognizerIntent
import android.view.* import android.view.*
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.view.* import androidx.core.view.*
import androidx.core.widget.doAfterTextChanged import androidx.core.widget.doAfterTextChanged
@ -53,7 +54,6 @@ class SearchFragment : AbsMainActivityFragment(R.layout.fragment_search),
ChipGroup.OnCheckedStateChangeListener { ChipGroup.OnCheckedStateChangeListener {
companion object { companion object {
const val QUERY = "query" const val QUERY = "query"
const val REQ_CODE_SPEECH_INPUT = 9001
} }
private var _binding: FragmentSearchBinding? = null private var _binding: FragmentSearchBinding? = null
@ -203,22 +203,17 @@ class SearchFragment : AbsMainActivityFragment(R.layout.fragment_search),
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()) intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt)) intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt))
try { try {
startActivityForResult( speechInputLauncher.launch(intent)
intent,
REQ_CODE_SPEECH_INPUT
)
} catch (e: ActivityNotFoundException) { } catch (e: ActivityNotFoundException) {
e.printStackTrace() e.printStackTrace()
showToast(getString(R.string.speech_not_supported)) showToast(getString(R.string.speech_not_supported))
} }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { private val speechInputLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result->
super.onActivityResult(requestCode, resultCode, data) if (result.resultCode == RESULT_OK) {
if (resultCode == RESULT_OK) {
val spokenText: String? = val spokenText: String? =
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) result?.data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)?.get(0)
.let { text -> text?.get(0) }
binding.searchView.setText(spokenText) binding.searchView.setText(spokenText)
} }
} }