FieldTask APIs¶
Introduction¶
To start a FieldTask activity:
- Create a new intent using an appropriate action.
- Set the type of the created intent.
- Start an activity using the intent.
Launching the form list¶
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android.cursor.dir/vnd.odk.form");
startActivity(intent);
This displays a list of forms and allows the user to select one and fill it in.
Launching the instance list¶
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android.cursor.dir/vnd.odk.instance");
startActivity(intent);
This displays a list of saved forms and allows the user to select one and edit it.
Getting the URI of a form or instance chosen by the user¶
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("vnd.android.cursor.dir/vnd.odk.form");
static final int PICK_FORM_REQUEST = 1; // The request code
startActivityForResult(intent, PICK_FORM_REQUEST);
To get the result, override onActivityResultMethod
in the following way:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_FORM_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The Intent's data URI identifies which form was selected.
Uri formUri = data.getData();
// Do something with the form here
}
}
}
For an instance, change the intent type:
intent.setType("vnd.android.cursor.dir/vnd.odk.instance");
Using a URI to edit a form or instance¶
If the URI of a form or instance is known, it can be viewed or edited. For example, a URI received in onActivityResult()
as described above can be used.
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData("content://org.odk.collect.android.provider.odk.forms/forms/2");
startActivity(intent);
The same thing can be done with a specific instance.