0

How to get the intent callback sharing result, i have seen some code and implemented from my side. Below is the sample code

try {
            Intent intent = new Intent(android.content.Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "Body");
            startActivityForResult(Intent.createChooser(intent, "Choose"), 1);
        } catch (Exception e) {
            e.printStackTrace();
        }

Code for callback result

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
        }
    }
}

In this requestCode is always getting 0 not 1 after i successfully share to any social media platform, is there any code which is newer version?

Mohammed Nabil
  • 662
  • 1
  • 8
  • 36

1 Answers1

0

The method you're using is deprecated, The new way is

ActivityResultLauncher<Intent> launcher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(), result -> {
        if (result.getResultCode() == Activity.RESULT_OK) {
            Intent data = result.getData();
            // do something
        }
    });

Launching the activity

launcher.launch(yourIntent);

sharing result

Intent intent = new Intent();
intent.putExtra(YOUR_KEY, YOUR_DATA);
setResult(RESULT_OK, intent);

Dependency

dependencies {
    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation "androidx.activity:activity:1.2.0"
    ...
}

To learn more about this visit

Sidharth Mudgil
  • 1,293
  • 8
  • 25