Skip to content
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

@ironjan

Description

@ironjan

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions