Faced such a task that you need to get the type of variable in one @IntDef , transfer it to another onActivityResult() via onActivityResult() .

The problem is that it is impossible to apply an explicit cast to this annotation, it cannot be serialized. If you create a field with getter \ setters, then the transfer returns an int (at best, sometimes you come across a String ).

int to @IntDef not applicable - because it is the basic principle and the whole salt of its use.

Let's say there is a certain @IntDef :

 import android.support.annotation.IntDef; @IntDef({Importance.noMatter, Importance.green, Importance.yellow, Importance.red}) public @interface Importance { public static int noMatter = 0; public static int green = 1; public static int yellow = 2; public static int red = 3; } 

Then, to use it, it is enough to write @Importance.green - and the necessary int will go to the input parameter that expects, for example, the method.

If we use just a number, the compiler will not allow us to do this. And he will not allow us to make the following construction:

  private Importance importance; public Importance getImportance() { return importance; } public void setImportance(Importance importance) { this.importance = importance; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == EDITING_REQUEST) { if (resultCode == RESULT_OK) { String importanceString = getIntent().getExtras().getString("importance"); switch (importanceString){ case "no_matter": setImportance(Importance.noMatter); break; } 

Thus, there are 2 options: either to artificially do something similar, complaining about the integrity of other developers, or to somehow solve this deadlock.

Of course, the first option is faster, but I still want to know if anyone has come across a similar one, or maybe I missed something \ didn’t see / misunderstood somewhere, and in fact there is a simple mechanism for this?

If Enum can at least be serialized as an object, I won’t attach my mind here.

As my good friend says - follow the path of a samurai.

    1 answer 1

    Pass such parameters to methods by annotating their type. You can put them in the Bundle in the usual way:

     public static Intent newIntent(@Importance int importance) { Intent intent = new Intent(context, MainActivity.class); intent.putExtra("Importance", importance); return intent; } 

    When pulling out the serialized variable, it is necessary to note the type of this also with the annotation + add a comment so that lint does not swear:

     //noinspection ResourceType @Importance int importance = getIntent().getIntExtra("Importance", Importance.noMatter); 
    • And how can a setter be made to the field? - Silento
    • According to the idea as well as in the method in the answer. Indicate the type of the parameter and the type of the field in the annotation - Yuriy SPb
    • It does not work, otherwise I would not ask. - Silento
    • he swears that I am transmitting an int, but he expects an annotation type. It just turns out that I can’t get him out of onActivityResult() - Silento
    • Ie here so will not work? @Importance int importance; public void setImportance(@Importance int importance) {this.importance = importance} @Importance int importance; public void setImportance(@Importance int importance) {this.importance = importance} ? I do not really understand your problem yet. Try asking a separate question - YuriySPb