0

It seems like I'm getting an inconsistent result code when creating an Intent with the Intent.ACTION_SEND action.
I've created a new project ("Basic Activity") and added the example from Google for "Sending simple data to other apps":

public class MainActivity extends AppCompatActivity {

  public static final int SHARE_REQUEST_CODE = 5684;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      FloatingActionButton fab = findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              share();
          }
      });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }

      return super.onOptionsItemSelected(item);
  }

  private void share() {
      Intent sendIntent = new Intent();
      sendIntent.setAction(Intent.ACTION_SEND);
      sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
      sendIntent.setType("text/plain");

      Intent shareIntent = Intent.createChooser(sendIntent, null);

      startActivityForResult(shareIntent, SHARE_REQUEST_CODE);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  }
}

My problem is that when copy (some devices have it as copy to clipboard) is selected, some devices (Pixel 4a, Android 11) get resultCode of Activity.RESULT_CANCELED (0) and not Activity.RESULT_OK (1) like in other devices (Nokia 3.1, Android 9).

Am I missing something or is it a bug?

MikeL
  • 2,756
  • 2
  • 23
  • 41
  • `ACTION_SEND` is not documented to return a result, so any result that you get is essentially random. – CommonsWare Nov 29 '20 at 16:28
  • The result seems consistent on each device, does that make sense? – MikeL Nov 29 '20 at 16:30
  • Copy-to-clipboard is going to be provided by the OS, so there will be one implementation per device. However, since there is not supposed to be a result, there is no guarantee what that result will be for that device. Also, the behavior on a particular device might change with OS updates. Also, there is no requirement for the user to choose copy-to-clipboard. – CommonsWare Nov 29 '20 at 16:41

0 Answers0