如何区分Uri结果和其他活动

Howto distinguish between Uri result and other activities

我的 Activity 中有一个 onActivityResult 方法。我得到 Bundle 并通过比较字符串值 ("function")

来区分不同的操作
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        // if (requestCode == REQUEST_QUICKLIST) {
        if (resultCode == Activity.RESULT_OK) {
            Bundle extras = data.getExtras();

            if (extras != null) {

                String sFunction = extras.getString("function");

现在我想额外处理新的 Lollipop 功能 Intent.ACTION_OPEN_DOCUMENT_TREE 以显示一个树形对话框,用户可以在其中选择一个文件夹来保存内容。此操作也将由 onActivityResult

处理
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (resultCode == RESULT_OK) {
        Uri treeUri = resultData.getData();
        DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);

如何区分我的普通动作和树动作?

Howto distinguish between my notmal actions and the tree action ?

在您的 startActivityForResult() 调用中使用不同的请求代码。这些将作为第一个参数传递给 onActivityResult(),您可以在 onActivityResult() 中使用逻辑来检查该请求代码并相应地进行分支。

将请求代码与 startActivityForResult(Intent intent, int requestCode);

一起使用

并在 onActivityResult()

if (resultCode == Activity.RESULT_OK)之前用它检查requestCode

喜欢,

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request you're responding to .. it differentiate your Activity and Tree action
    if (requestCode == <YOUR_ACTIVITY_REQUEST_CODE>) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {

            // Do something with the result
        }
    }
}