This repository was archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
@Extra - generate public class fields for extras instead of hardcoding extra name #441
Copy link
Copy link
Closed
Description
What happens right now
Right now AndroidAnnotations generates hardcoded strings when using @Extras. This leads to hardcoding the extra's name in Fragments that get the extra passed by an Activity.
What should happen
Generate a public class constant that holds the extra's name. This constant can be used in the generated activity instead of hardcoding and in fragments that get the extras passed.
Code Examples
Base File
@EActivity
public class NewPostActivity extends SherlockFragmentActivity {
@Extra
String photoUri;
}Generated
private void injectExtras_() {
Intent intent_ = getIntent();
Bundle extras_ = intent_.getExtras();
if (extras_!= null) {
if (extras_.containsKey("photoUri")) {
try {
photoUri = cast_(extras_.get("photoUri"));
} catch (ClassCastException e) {
Log.e("NewPostActivity_", "Could not cast extra to expected type, the field is left to its default value", e);
}
}
}
}
public NewPostActivity_.IntentBuilder_ photoUri(String photoUri) {
intent_.putExtra("photoUri", photoUri);
return this;
}How it should be
public static String photoUriExtra = "photoUri";
private void injectExtras_() {
Intent intent_ = getIntent();
Bundle extras_ = intent_.getExtras();
if (extras_!= null) {
if (extras_.containsKey(photoUriExtra)) {
try {
photoUri = cast_(extras_.get(photoUriExtra));
} catch (ClassCastException e) {
Log.e("NewPostActivity_", "Could not cast extra to expected type, the field is left to its default value", e);
}
}
}
}
public NewPostActivity_.IntentBuilder_ photoUri(String photoUri) {
intent_.putExtra(photoUriExtra, photoUri);
return this;
}in a fragment
// before solving this issue
// if extra's name changes this variable can be easily forgotten
final String photoUriExtra = "photoUri";
// after change
final String photoUriExtra = NewPostActivity_.photoUriExtra;Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels