可以在该onActivityResult(int requestCode, int resultCode, Intent data)方法中读取语音识别的结果,如该示例中所示.这个方法在类中重写了相同的方法Activity:为什么对超类方法的调用不是第一个语句?
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
// ...
}
super.onActivityResult(requestCode, resultCode, data);
}
Run Code Online (Sandbox Code Playgroud) 注意:在执行任何工作之前,您对这些生命周期方法的实现必须始终调用超类实现...
但我已经看到过代码放在超类方法之后的情况,特别是对于像onPause(),onStop(),onDestroy()这样的方法,例如:
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
Run Code Online (Sandbox Code Playgroud)
它在两种方式都有效.那么,在调用超类方法之后将代码置于o之前有什么区别?什么是正确的方法?