I am programming a speech recognition app in Kotlin for Android.
class MainActivity : AppCompatActivity() { public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val intent:Intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) startActivityForResult(intent, REQUEST_CODE) } override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent) { if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {/*do something*/ } super.onActivityResult(requestCode, resultCode, data) } }
Strangly the compiler always finds the Error: 'onActivityResult' overrides nothing.
Documentation of Android states that result of startActivityForResult can be retrived with onActivityResult.
Now the question: how can one get the result of speech recognition using Kotlin?
onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.
onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.
Replace
override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent)
With below code, to make Intent object nullable.
override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent?)
As Intent is declared nullable in parent Activity class. Here is the sample code:
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With