title: Update to androidx.fragment:fragment:1.3.0-alpha08: registerForActivityResult not allowed after onCreate anymore. How to use after onCreate?
body: <h1>Initial Question (18/05/2020):</h1>
  <p>So with the latest Update from</p>
  <ul>
  <li>androidx.fragment:fragment:1.3.0-<strong>alpha07</strong></li>
  </ul>
  <p>to</p>
  <ul>
  <li>androidx.fragment:fragment:1.3.0-<strong>alpha08</strong></li>
  </ul>
  <p>I get the error:</p>
  <blockquote>
  <p>FragmentXY is attempting to registerForActivityResult after being created. Fragments must call registerForActivityResult() before they are created (i.e. initialization, onAttach(), or onCreate()).</p>
  </blockquote>
  <p>I used to check permissions in my StartFragment (Single Activity App, in onViewCreated) after showing to the User information about the use of those permissions and why they are needed. Everything worked perfectly for the last 3(?) months.</p>
  <p>I see in the <a href="https://developer.android.com/jetpack/androidx/releases/fragment#1.3.0-alpha08" rel="noreferrer">changelog</a>:</p>
  <blockquote>
  <h3>Behavior Changes</h3>
  <p>[...]<br />
  Calling registerForActivityResult() after onCreate() now throws an exception indicating that this is not allowed rather than silently failing to deliver results after a configuration change. (b/162255449)
  &quot;</p>
  </blockquote>
  <p>I downgraded back to version 1.3.0-alpha07 for the moment.<br />
  But if I need <strong>registerForActivityResult</strong> in my Fragments <strong>AFTER</strong>  the view is created (e.g. for permissions), how can I do it when upgrading to version 1.3.0-alpha08?</p>
  <p>The docs state that I should use launch() in onCreate of my Fragment (see below) but that would mean I have to do it before the view is created, and that would be contradictory to my app flow.</p>
  <blockquote>
  <h3>Behavior Changes</h3>
  <p>[...]<br />
  You can now call launch() on an ActivityResultLauncher in the onCreate() lifecycle method of a fragment. (b/161464278)
  &quot;</p>
  </blockquote>
  <p>As this behaviour seems to be intended by the developers, it is not a bug or anything but how can I continue using ActivityResults after onCreate? Any ideas?</p>
  <hr />
  <h1>Edit (19/05/2020):</h1>
  <p>Thanks to @A.Andriyishyna I understand that registration (in onCreate) and execution (when needed, e.g. in onViewCreated) have to be handled separately.
  Problem is that I have handy inline functions (with courtesy to <a href="https://github.com/Flywith24/Flywith24-ActivityResultRequest/tree/15bfacf6a7d02e0467d7c0edd3813c83c5dd98f4" rel="noreferrer">Flywith24</a>) in other files, which help me to separate the permission BL from the View (Fragment).<br />
  Is there a way to keep those inline functions without having to change them drastically?</p>
  <ol>
  <li>Fragment</li>
  </ol>
  <pre><code>class GalleryFragment: ScopedFragment() {
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          initializePermissions(requiredContext)
      }
  
      private fun initializePermissions(context: Context) {
          storagePermissions(
              context = context,
              actionOnGranted = { showImages() },
              actionOnDeclined = { showNoAccess() },
              actionRepeat = { initializePermissions(context) }
          )
      }
  }
  </code></pre>
  <ol start="2">
  <li>PermissionDSL</li>
  </ol>
  <pre><code>inline fun Fragment.storagePermissions(
      context: Context,
      crossinline actionOnGranted: () -&gt; Unit,
      crossinline actionOnDeclined: () -&gt; Unit,
      crossinline actionRepeat: () -&gt; Unit
  ) {
      when {
          Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.Q -&gt; {
  
              if (
                  ContextCompat.checkSelfPermission(
                      context, Manifest.permission.READ_EXTERNAL_STORAGE
                  ) == PackageManager.PERMISSION_GRANTED
              ) {
                  actionOnGranted()
              } else {
                  permission(
                      Manifest.permission.READ_EXTERNAL_STORAGE
                  ) {
                      granted = {
                          actionOnGranted()
                      }
                      denied = {
                          actionRepeat()
                      }
                      explained = {
                          actionOnDeclined()
                      }
                  }
              }
          }
  
          Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.Q -&gt; {
              if (
                  ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_MEDIA_LOCATION
                  ) == PackageManager.PERMISSION_GRANTED) {
                  Log.d(&quot;Storage Permission&quot;, &quot;Permission already granted.&quot;)
                  actionOnGranted()
              } else {
                  Log.d(&quot;Storage Permission&quot;, &quot;No Permission Yet -&gt; Ask for it!&quot;)
                  permissions(
                      Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_MEDIA_LOCATION
                  ) {
                      allGranted = {
                          actionOnGranted()
                      }
                      denied = {
                          Log.d(&quot;Storage Permission&quot;, &quot;Denied&quot;)
                          actionRepeat()
                      }
                      explained = {
                          Log.d(&quot;Storage Permission&quot;, &quot;Permanently Denied&quot;)
                          actionOnDeclined()
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  <ol start="3">
  <li>PermissionExtension</li>
  </ol>
  <pre><code>inline fun Fragment.requestPermission(
      permission: String,
      crossinline granted: (permission: String) -&gt; Unit = {},
      crossinline denied: (permission: String) -&gt; Unit = {},
      crossinline explained: (permission: String) -&gt; Unit = {}
  
  ) {
      registerForActivityResult(ActivityResultContracts.RequestPermission()) { result -&gt;
          when {
              result -&gt; granted.invoke(permission)
              shouldShowRequestPermissionRationale(permission) -&gt; denied.invoke(permission)
              else -&gt; explained.invoke(permission)
          }
      }.launch(permission)
  }
  
  
  inline fun Fragment.requestMultiplePermissions(
      vararg permissions: String,
      crossinline allGranted: () -&gt; Unit = {},
      crossinline denied: (List&lt;String&gt;) -&gt; Unit = {},
      crossinline explained: (List&lt;String&gt;) -&gt; Unit = {}
  ) {
      registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result: MutableMap&lt;String, Boolean&gt; -&gt;
  
          val deniedList = result.filter { !it.value }.map { it.key }
          when {
              deniedList.isNotEmpty() -&gt; {
  
                  val map = deniedList.groupBy { permission -&gt;
                      if (shouldShowRequestPermissionRationale(permission)) DENIED else EXPLAINED
                  }
  
                  map[DENIED]?.let { denied.invoke(it) }
  
                  map[EXPLAINED]?.let { explained.invoke(it) }
              }
              else -&gt; allGranted.invoke()
          }
      }.launch(permissions)
  }
  </code></pre>