android - 对父类(super class)方法的调用应该是第一条语句吗?

标签 android android-activity android-lifecycle

可以在onActivityResult(int requestCode, int resultCode, Intent data)方法中读取语音识别的结果,如this example所示.该方法覆盖了类Activity中的相同方法:为什么调用父类(super class)方法不是第一条语句?

@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);
}

最佳答案

作为组件创建一部分的您覆盖的方法(onCreate()onStart()onResume() 等),您应该链接到父类(super class)作为第一条语句,以确保在您尝试执行依赖于已完成工作的事情之前,Android 有机会完成其工作。

您覆盖的作为组件销毁一部分的方法(onPause()onStop()onDestroy() 等),你应该先做你的工作,最后链接到父类(super class)。这样一来,如果 Android 清理了您的工作所依赖的某些内容,您将首先完成您的工作。

返回 void 以外的东西的方法(onCreateOptionsMenu() 等),有时你在 return 语句中链接到父类(super class),假设你不是专门做一些需要强制返回特定值的事情。

其他一切——例如 onActivityResult()——总体上取决于您。我倾向于首先链接到父类(super class),但除非您遇到问题,否则稍后链接应该没问题。

关于android - 对父类(super class)方法的调用应该是第一条语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625920/

相关文章:

android - 如何调用 setOnItemClickListener()

android - 如何在抽屉导航中为 ListView 设置自定义 EmptyView

java - android中 Activity 生命周期的父类(super class)实现是做什么的?

android - 在 android 中的 TabHost + fragment 中调用 oncreateView 时出错?

java - Android Fragment 有多少开销?

Android:相当于IOS的 "applicationDidEnterBackground()"

android - clipPath 上的 UnsupportedOperationException

安卓 Kotlin : Required Context but found String

android - 无法销毁 Activity (服务未注册)

java - Android "single top"启动模式和onNewIntent方法