/bundle-injector

Android library for injecting parameters from Bundle

Primary LanguageJavaMIT LicenseMIT

Bundle Injector

Android library for injecting parameters from Bundle. It helps you reduce code needed to retrieve values from Bundle.

Example

public class YourActivity extends Activity {

    @InjectBundle
    String itemId;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // inject itemId from extras Bundle
        BundleInjector.inject(this, getIntent().getExtras());
    }
}

Setup

Eclipse: In Eclipse, import the BundleInjectorLibrary as Android library project. If needed, fix build path, so src/main/java is the source folder. Finally, add dependency to the project via project settings.

Android Studio: In Android Studio, import the BundleInejctorLibrary module via File -> Import module. If you don't have dependency classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'in your main build.gradle, add it there, or remove apply plugin: 'android-test' and testCompile, androidTestCompile dependencies from build.gradle in BundleInjectorLibrary

Usage

IMPORTANT: each injected variable must have exactly the same name in the Bundle

Simple example showing data exchange between 2 activities:

ActivityA.java

public class ActivityA extends Activity {

    @InjectBundle
    String returnedParam;

    public void onCreate(Bundle savedInstanceState) {
        ...
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(ActivityA.this, ActivityB.class);
                Bundle extras = new Bundle();
                extras.putString("passedParam", "SAMPLE VALUE");
                intent.putExtras(extras);
                startActivityForResult(intent, 0);
            }
        });
        ...
    }
    
     public void onActivityResult(int reqCode, int resCode, Intent data) {
        ...
        BundleInjector.inject(this, data.getExtras());
        // now, returnedParam contains value from Activity B
        ...
     }

}

ActivityB.java

public class ActivityB extends Activity {

    @InjectBundle
    String passedParam;

    public void onCreate(Bundle savedInstanceState) {
        ...
        BundleInjector.inject(this, getIntent().getExtras());
        // now, passedParam contains value from Activity A
        ...
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent result = new Intent();
                Bundle extras = new Bundle();
                extras.putString("returnedParam", "SAMPLE RETURNED VALUE");
                result.putExtras(extras);
                setResult(RESULT_OK, result);
                finish();
            }
        });
        ...
    }

}

Caveats

If you have generic class and injecting field is parametrized, make sure you put correct type into Bundle, since type check is failing when using generics.

For more information, see stackoverflow thread