Android 在kotlin枚举中的应用

Android 在kotlin枚举中的应用,android,kotlin,enums,android-application-class,Android,Kotlin,Enums,Android Application Class,在kotlin枚举中使用应用程序是否安全?像这样: enum class Labels(title: String, type: Int) { PERFORM(App.application.getString(R.string.perform), 0), DUTY(App.application.getString(R.string.duty), 1), ... ... } 我不会在枚举中使用App类。相反,我只传递资源id,因为我们不能依赖于在第一次加载enu

在kotlin枚举中使用应用程序是否安全?像这样:

enum class Labels(title: String, type: Int) {
     PERFORM(App.application.getString(R.string.perform), 0),
     DUTY(App.application.getString(R.string.duty), 1),
     ... ...
}
我不会在枚举中使用App类。相反,我只传递资源id,因为我们不能依赖于在第一次加载enum类时实例化了App类:

enum class Labels(val titleResId: Int, val type: Int) {
   PERFORM(R.string.perform, 0),
   DUTY(R.string.duty, 1),
   ... ...
}
稍后我们可以在活动中使用它,例如:

textView.setText(Labels.PERFORM.titleResId)

据我所知,您唯一需要担心的是在首次加载枚举类时是否可以检索这些值。非常感谢您的回复,我将修改我的代码。有人告诉我,enum是一个静态字段,所以我不应该将上下文放在其中