title: How to handle createChooser's IntentSender without having the component class of the receiver Intent
body: <p>I am trying to handle the <code>IntentSender</code> of <code>Intent.createChooser()</code> to do something when a user selects an app to share an image on. Most the examples I've found here (posted below), require using a <code>BroadcastReceiver</code> <a href="https://medium.com/code-with-lisa/get-results-from-android-chooser-9cfc5445a871" rel="nofollow noreferrer">as follows</a>:</p>
  
  <pre><code>Intent receiver = new Intent(context, MyReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
  String type = "image/*";
  Intent share = new Intent(Intent.ACTION_SEND);
  share.setType(type);
  share.putExtra(Intent.EXTRA_STREAM, awesome_photo_uri);
  startActivity(Intent.createChooser(share, "some_title", pendingIntent.getIntentSender()));
  </code></pre>
  
  <p>My problem with this solution, is located in this line: </p>
  
  <pre><code>Intent receiver = new Intent(context, MyReceiver.class);
  </code></pre>
  
  <p>The <a href="https://developer.android.com/reference/android/content/Intent.html#Intent(android.content.Context,%20java.lang.Class%3C?%3E)" rel="nofollow noreferrer">Intent constructor</a> used in these examples require me to make a static <code>MyReceiver</code> class, so I have a class to pass into the second argument of the constructor. But, this causes an issue because <strong>I'd like the <code>BroadcastReceiver</code>'s <code>onReceive</code> to do stuff in my Fragment.</strong> Therefore, I would prefer to create a <code>BroadcastReceiver</code> dynamically in my Fragment.</p>
  
  <p>To no avail, I attempted the following work-around:</p>
  
  <p>Inside <code>MyFragment.kt</code>:</p>
  
  <pre><code>private val receiver: BroadcastReceiver = getBroadcastReceiver()
  private val intentFilter = IntentFilter("com.my.app.CHOOSER_ACTION")
  
  override fun onResume() {
      requireContext().registerReceiver(receiver, intentFilter)
      super.onResume()
  }
  
  override fun onPause() {
      requireContext().unregisterReceiver(receiver)
      super.onPause()
  }
  
  private fun shareImage(imageFile: File) {
      Intent().apply {
          flags = Intent.FLAG_ACTIVITY_NEW_TASK
          action = Intent.ACTION_SEND
          type = "image/*"
          putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(
              requireContext(),
              "${myPackageName}.fileprovider",
              imageFile
          ))
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP_MR1) {
              val receiver = Intent("com.my.app.CHOOSER_ACTION")
              val pendingIntent = PendingIntent.getBroadcast(requireContext(), 0, receiver, PendingIntent.FLAG_CANCEL_CURRENT)
              startActivity(Intent.createChooser(this, "Share image using", pendingIntent.intentSender))
          } else {
              startActivity(Intent.createChooser(this, "Share image using"))
          }
      }
  }
  
  private fun getBroadcastReceiver() : BroadcastReceiver {
      return object : BroadcastReceiver() {
          override fun onReceive(context: Context?, intent: Intent?) {
              intent?.extras?.keySet()?.forEach {
                  Log.v("MyTest", "$it: ${intent.extras?.get(it)}")
              }
              doSomethingInMyFragment()
          }
      }
  }
  </code></pre>
  
  <p>Inside <code>AndroidManifest.xml</code>:</p>
  
  <pre><code>&lt;activity android:name="MyActivityThatHasMyFragment" /&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="com.my.app.CHOOSER_ACTION" /&gt;
      &lt;/intent-filter&gt;
  &lt;/activity&gt;
  </code></pre>
  
  <p>Unfortunately, the dynamic <code>BroadcastReceiver</code>'s <code>onReceive()</code> function is never called after the user presses on a selection. A few questions:</p>
  
  <ol>
  <li>Why does this not work? What am I missing? Am I somehow setting the <code>Intent</code> or <code>IntentFilter</code> incorrectly?</li>
  <li>Is it even possible to use a dynamic <code>BroadcastReceiver</code> for handling the <code>IntentSender</code> of <code>createChooser</code>? If not, how can I create a static <code>BroadcastReceiver</code> that triggers something to happen in <code>MyFragment</code>?</li>
  </ol>
  
  <p><strong>Resources:</strong></p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/30631902/get-intentsender-object-for-createchooser-method-in-android">Get IntentSender object for createChooser method in Android</a></li>
  <li><a href="https://medium.com/code-with-lisa/get-results-from-android-chooser-9cfc5445a871" rel="nofollow noreferrer">Get results from Android Chooser</a></li>
  <li><a href="https://stackoverflow.com/questions/32203230/how-to-tell-which-app-was-selected-by-intent-createchooser">How to tell which app was selected by Intent.createChooser?</a></li>
  <li><a href="https://stackoverflow.com/questions/9583230/what-is-the-purpose-of-intentsender">What is the purpose of IntentSender?</a></li>
  <li><a href="https://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent,%20java.lang.CharSequence,%20android.content.IntentSender)" rel="nofollow noreferrer">Intent.createChooser()</a></li>
  </ul>