Skip to content

Optional argument

Marcin Moskała edited this page Jul 30, 2017 · 1 revision

@Optional annotation is used then we want to generate the variant of starter methods (start, getIntent, etc.) without one or more arguments. These arguments will be left with default values. This is a simple example:

public class MainActivity extends Activity {
    @Arg int id;
    @Arg @Optional String name = "Marcin";
}

MainActivity defined this way can be both started this two ways:

MainActivityStarter.start(context, id) // Field name will be left as "Marcin"
MainActivityStarter.start(context, id, "Maja") // Field name will be set to "Maja"

The same works with Fragments, Services and all other supported classes. Note, that because of naming conflict, in this situation:

public class MainActivity extends Activity {
    @Arg @Optional String name = "Marcin";
    @Arg @Optional String surname = "Moskala";
}

To prevent excistance of two functions with the same signature, the possible usages will be only:

MainActivityStarter.start(context)
MainActivityStarter.start(context, name)
MainActivityStarter.start(context, name, surname)