Skip to content

Hardcoded activity result identifiers #46

@deakjahn

Description

@deakjahn

Prefer not to use hardcoded identifiers for activity results:

companion object {
const val REQUEST_CODE_OPEN_FILE = 40832
const val REQUEST_CODE_CREATE_FILE = 40833
}

Use IDs in res/values/ids.xml instead. That way, you can avoid any possible collision because the compiler toolchain will generate unique IDs even if the app has its own and the plugins it relies on also have their own.

It's as simple as:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <item name="some_request_name" type="id" />
</resources>

and then using it:

activity.startActivityForResult(intent, R.id.some_request_name and 0xFFFF)

You're free to and it with 0xFFFF, the unique ID is in the lower word and startActivityForResult() expects a 16-bit value, anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions