I have an app that uses the ACTION_RECOGNIZE_SPEECH intent. It works fine when i call startActivityForResult from an Activity and the corresponding onActivityResult is in the same Activity.
My problem is i have another app that uses fragments. If i call startActivityForResult from the Fragment, the speech box does not appear.
What i have tried:
I've tried overridding onActivityResult in the parent Activity and then call getActivity.startActivityForResult(intent, SPEECH_REQUEST_CODE); This just launches the intent and the onActivityForResult in the Activity class executes.
i've tried various combinations from the following post.
onActivityResult is not being called in Fragment
This is the code i have at the moment and the logs. Has anyone any suggestions?
NB i have the following permissions in the manifest too:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET" />
.
Parent Activity:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e(TAG, "inside onActivityForResult in parent. requestCode = " + requestCode + "resultCode = " + requestCode + " data = " + data );
}
.
Fragment class:
private static final int SPEECH_REQUEST_CODE = 0;
// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
Log.e(TAG, "inside displaySpeechRecognizer() and speechRequestCode = " + SPEECH_REQUEST_CODE);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e(TAG, "inside onActivityForResult in child fragment. requestCode = " + requestCode + " resultCode = " + resultCode);
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
// Do something with spokenText
Log.e(TAG, "spokenText = " + spokenText);
etCommentEditText.append(" " + spokenText);
}
super.onActivityResult(requestCode, resultCode, data);
}
.
Logs:
05-02 14:57:37.012 14414-14414/com.carefreegroup.rr3.carefreeoncall E/AlertDetailsFragment: inside displaySpeechRecognizer() and speechRequestCode = 0
05-02 14:57:37.072 14414-14414/com.carefreegroup.rr3.carefreeoncall E/AlertDetailsFragment: inside onActivityForResult in child fragment. requestCode = 0 resultCode = 0