片段中的AndroidAnnotations

时间:2018-01-09 21:31:39

标签: java android android-annotations

是否可以制作

@Receiver(action ={MY_ACTION], local = true)
protected void MyMethod{
myMethod()
}

进入片段? 它仅适用于活动......

Android工作室仅获取此信息:Annotations are not allowed here

我忘记了什么?

1 个答案:

答案 0 :(得分:1)

你可能已经有了这个权利,但为了以防万一,首先确认你使用的是Android版本3.1或更高版本,这是一个example gradle file from the github project使用最新的稳定版本(4.4.0):

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        // replace with the current version of the Android plugin
        classpath "com.android.tools.build:gradle:3.0.0"
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

apply plugin: "com.android.application"

def AAVersion = "4.4.0" // change this to your desired version, for example the latest stable: 4.4.0
dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    implementation "org.androidannotations:androidannotations-api:$AAVersion"
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 26

        // If you have different applicationIds for buildTypes or productFlavors uncomment this block.
        //javaCompileOptions {
        //    annotationProcessorOptions {
        //        arguments = ['resourcePackageName': "org.androidannotations.sample"]
        //    }
        //}
    }
}

然后应用@EFragment并整理片段类中的@Receiver注释,

@EFragment
public class MyFragment extends Fragment {

  @Receiver(actions = "MY_ACTION", local = true)
  protected void myMethod() {
    // Will be called when an MY_ACTION intent is sent.
  }

}

有关更多信息/文档以及一个很棒的@EFragment示例: https://github.com/androidannotations/androidannotations/wiki/Enhance-Fragments

希望这有帮助!