Skip to content

Usage for Activities

MarcinMoskala edited this page Feb 10, 2017 · 5 revisions

Fully functional example of Java and Kotlin Android applications are here. There are simple and tested.

Simplest example for Activity usage is just by adding @MakeActivityStarter before class. It is used when there is no arguments we would like to provide, and we want to have starter for starting this Activity:

@MakeActivityStarter
public class MainActivity extends Activity {}

When there are arguments, then we don't need @MakeActivityStarter annotation. Each field that we want to provide by argument we need to annotate with @Arg annotation.

public class MainActivity extends Activity {
    @Arg String name;
}

Node that this field doesn't need to be public, when its accessors are:

public class MainActivity extends Activity {
    @Arg private String name;

    public void setName(String name) {
        this.name = name;
    }
}

This case is especially important in Kotlin, when by default fields are private with public accessors.

Also, to fill this fields, class needs to start fill function from ActivityStarter class, before usage of any of this arguments. It is suggested to do it on onCreate method, or event better, on BaseMethod from which all project Activities inherit:

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityStarter.fill(this);
}

}

Class can contain multiple arguments:

public class StudentDataActivity extends BaseActivity {

    private static int NO_ID = -1;

    @Arg String name;
    @Arg int id;
    @Arg char grade;
    @Arg boolean isPassing;

This are options to start it:

StudentDataActivityStarter.start(context, name, id, grade, isPassing);
StudentDataActivityStarter.startWithFlags(context, name, id, grade, isPassing, flags);

Intent intent = StudentDataActivityStarter.getIntent(context, name, id, grade, isPassing);
startActivity(intent);

Usage in Kotlin is pretty the same. No need for @JvmField thanks to setter accessibility. Fields must be var. If field in not [@Optional](@Optional annotation) then it should be annotated with lateinit (no need for that, but there is also no need for setting value which will be overridden or for making field nullable).