title: How to get result using registerForActivityResult from within ktor's Routing call running in a non-activity class?
body: <p>How to get result from another activity (registerForActivity) from with in ktor's Routing API call (eg. /POST) running in a non-activity class?</p>
  <p>Background: For an Android app, I run ktor server engine 'netty' in a non-activity class HttpServer.kt. I need to call another app's activity from with in ktor's Routing' POST handler, so I pass 'appCompatActivity' from MainActivity.kt. That's done, just because, I assume, registerForActivityResult() has dependency on UI/life cycle class.</p>
  <p>Problem arises when running this as below, as registerForActivityResult() requires to be run earlier (like onCreate() ?), and I don't have such a class in this non-activity class. Moreover, the callback to run when ActivityResult is returned needs to call ktor  ApplicationCall's respond which is also a suspend function.</p>
  <pre><code>class HttpServer(
      private val applicationContext: AppCompatActivity
  ) {
      private val logger = LoggerFactory.getLogger(HttpServer::class.java.simpleName)
      private val server = createServer()
  
      private fun ApplicationCall.startSaleActivityForResult() {    //    &lt;======   *
          val activityLauncherCustom =
              applicationContext.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult -&gt;
                  if (result.resultCode == Activity.RESULT_OK || result.resultCode == Activity.RESULT_CANCELED) {
                      val transactionResultReturned = result.data
                      // Handle the returned result properly using transactionResultReturned
                      GlobalScope.launch {
                          respond(status = HttpStatusCode.OK, TransactionResponse())
                      }
                  }
              }
          val intent = Intent()    
          // Ignoring statements to create proper action/data intent
          activityLauncherCustom.launch(intent)    //    &lt;======   *
      }
  
      fun start() = server.start()
  
      fun stop() = server.stop(0, 0)
  
      private fun createServer(): NettyApplicationEngine {
          return GlobalScope.embeddedServer(Netty) {
              install(CallLogging)
              install(ContentNegotiation) {
                  gson {
                      setPrettyPrinting()
                  }
              }
              routing {
                  route(&quot;/&quot;) {
                      post {
                          call.startSaleActivityForResult()    //    &lt;======   *
                      }
                  }
              }
          }
      }
  
      private fun &lt;TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configuration&gt;
              CoroutineScope.embeddedServer(
          factory: ApplicationEngineFactory&lt;TEngine, TConfiguration&gt;,
          module: Application.() -&gt; Unit
      ): TEngine {
          val environment = applicationEngineEnvironment {
              this.parentCoroutineContext = coroutineContext + parentCoroutineContext
              this.log = logger
              this.module(module)
  
              connector {
                  this.port = 8081
              }
          }
          return embeddedServer(factory, environment)
      }
  }
  
  </code></pre>
  <p>Above is what I tried, but gives below error. And I don't have onCreate on this non-activity class.</p>
  <blockquote>
  <p>java.lang.IllegalStateException: LifecycleOwner com.youtap.upti.MainActivity@38dcf06 is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.</p>
  </blockquote>
  <p>Any suggestions to resolve this problem would be grateful.</p>
  <p>Below same above snippet as a screenshot to display helper text on declaration/param types from Android Studio:
  <a href="https://i.stack.imgur.com/ZR0gU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZR0gU.png" alt="enter image description here" /></a></p>
  <p>And I invoke this server class from onCreate() of MainActivity:
  <a href="https://i.stack.imgur.com/pFVjn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pFVjn.png" alt="enter image description here" /></a></p>