IntentBuilder

repository·master·Indexed 43 days ago

https://github.com/emilsjolander/intentbuilder

A library for Android that provides type-safe intent building and extra injection for Activities and Services. It uses annotation processing via @IntentBuilder and @Extra to generate builder classes, reducing risks associated with dynamic intent extras.

Tokens
1.4K
Snippets
4
Records
6
Agent score
6%

What's inside IntentBuilder

  1. How IntentBuilder works: Annotations and Generated Classes

    master

    IntentBuilder uses annotation processing to provide type-safe intent creation.

    1. Annotate: Mark your Activity or Service classes with the @IntentBuilder annotation.
    2. Generation: The library automatically generates a builder class named [YourClassName]IntentBuilder. For example, DetailActivity results in DetailActivityIntentBuilder.
    3. Define Extras: Use the @Extra annotation on fields within your Activity or Service to define which intent extras should be mapped to those fields.
    4. Optional Extras: Use @Extra @Nullable to mark an extra as optional.
    5. Injection: Use the static inject(Intent intent, T target) method on the generated builder class to populate the annotated fields of your Activity or Service instance from an incoming Intent.
  2. Install IntentBuilder in your Gradle project

    master

    To use IntentBuilder, you need to apply the android-apt plugin and add both the intentbuilder-api and intentbuilder-compiler dependencies to your project. The compiler is required as an apt dependency to generate the builder classes at compile time.

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
        }
    }
    
    apply plugin: 'com.neenbedankt.android-apt'
    
    dependencies {
        compile 'se.emilsjolander:intentbuilder-api:0.14.0'
        apt 'se.emilsjolander:intentbuilder-compiler:0.14.0'
    }
  3. How IntentBuilder works with annotations

    master

    IntentBuilder uses code generation to provide type-safe intent creation.

    1. Enable Generation: Annotate your Activity or Service class with @IntentBuilder.
    2. Generated Class: For a class named MyActivity, the library generates a class named MyActivityIntentBuilder.
    3. Define Extras: Mark fields within your Activity/Service with @Extra to indicate they should be populated from Intent extras. Use @Nullable on these fields to mark them as optional.
    4. Injection: Inside the onCreate (for Activities) or onHandleIntent (for Services), call the static inject(Intent intent, T target) method on the generated builder class to populate the annotated fields.
  4. Install IntentBuilder in your Android project

    master

    To use IntentBuilder, you must configure your Gradle build script to include the API and the annotation processor (APT). This requires applying the android-apt plugin and adding the intentbuilder-api and intentbuilder-compiler dependencies. Replace %%version%% with the appropriate version of the library.

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
        }
    }
    
    apply plugin: 'com.neenbedankt.android-apt'
    
    dependencies {
        compile 'se.emilsjolander:intentbuilder-api:%%version%%'
        apt 'se.emilsjolander:intentbuilder-compiler:%%version%%'
    }
  5. Use IntentBuilder in a Service

    master

    To use IntentBuilder in a Service, annotate the class, mark fields with @Extra, and call inject within the service's intent handling method (e.g., onHandleIntent for IntentService). Use the generated builder to start the service.

    @IntentBuilder
    class DownloadService extends IntentService {
    
        @Extra
        String downloadUrl;
    	
    	@Override
        protected void onHandleIntent(Intent intent) {
            DownloadServiceIntentBuilder.inject(intent, this);
        }
    
    }
    
    // To start this service:
    startService(new DownloadServiceIntentBuilder("http://google.com").build(context))
  6. Use IntentBuilder in an Activity

    master

    To use IntentBuilder in an Activity, annotate the class, mark fields with @Extra, and call inject in onCreate. To start the activity, use the generated builder class to set values and call .build(context).

    @IntentBuilder
    class DetailActivity extends Activity {
    	
    	@Extra
    	String id;
    
    	@Extra @Nullable
    	String title;
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		DetailActivityIntentBuilder.inject(getIntent(), this);
    		// TODO use id and title
    	}
    
    	// To start this activity:
    	startActivity(new DetailActivityIntentBuilder("12345")
    		.title("MyTitle")
    		.build(context))
    }