Search code examples
androidandroid-intentstorage-access-framework

Passing extra data through the Storage Access Framework


I have a ListView and from a click on an item of the ListView I call the Storage Access Framework (SAF).

Just before starting the SAF, I want to pass the position of the ListView like this:

intent.putExtra("Position", position);
startActivityForResult(intent, WRITE_REQUEST_CODE);

But how do I get the position back in the onActivityResult() method so I know which item is clicked?

I tried these lines, but with no success:

Bundle extras = resultData.getExtras();
Integer i = getIntent().getIntExtra("routeDataPosition", 42);
Integer i2 = resultData.getIntExtra("routeDataPosition", 42);

Solution

  • Just before starting the SAF, I want to pass the position of the ListView like this:

    That does not work. You cannot force extras into another app, and you cannot force another app to return those extras.

    But how do I get the position back in the onActivityResult() method so I know which item is clicked?

    You could store it in a field in your activity, fragment, or viewmodel.

    If you wanted to get elaborate, you could use a different request code based on the position. So, instead of WRITE_REQUEST_CODE, use WRITE_REQUEST_CODE + position. You would need to have smarts in onActivityResult() to get the position back out of the result code (e.g., position = resultCode - WRITE_REQUEST_CODE).