Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
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
@ironjan

Description

@ironjan
Issue body actions

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.