Skip to content

customTab.onNavigationEvent issue in production #157

@arlee17

Description

@arlee17

Hello,

Users navigate to a custom tab to pay in my e-commerce app, so we have implemented a CustomBrowserTabDecisionHandler.
We have many errors in this implementation. Here is my code:

  override val name: String = "custom-browser-tab"
  private var customTabsSession: CustomTabsSession? = null
  private var customTabsConnection: CustomTabsServiceConnection? = null

  override fun matches(
    location: String,
    configuration: NavigatorConfiguration
  ): Boolean {
    return configuration.startLocation.toUri().host != location.toUri().host
  }

  @SuppressLint("ResourceAsColor")
  override fun handle(
    location: String,
    configuration: NavigatorConfiguration,
    activity: HotwireActivity
  ): Router.Decision {
    if (customTabsConnection != null) {
      try {
        activity.unbindService(customTabsConnection!!)
      } catch (e: IllegalArgumentException) {
        Log.w("CUSTOM_TAB", "unbindService failed: ${e.message}")
      }
      customTabsConnection = null
    }

    customTabsConnection = object : CustomTabsServiceConnection() {
      val customTabsCallback = object : CustomTabsCallback() {
        override fun onNavigationEvent(navigationEvent: Int, extras: Bundle?) {
          when (navigationEvent) {
            TAB_HIDDEN -> {
              try {
                activity.unbindService(customTabsConnection!!)
              } catch (e: IllegalArgumentException) {
                Log.w("CUSTOM_TAB", "unbindService failed: ${e.message}")
              }

              customTabsConnection = null

              MainActivity.instance.navigateTo(getBaseUrl())
              activity.delegate.resetSessions()
            }

            else -> Log.d("NAVIGATION_EVENT", "Unknown event")
          }
        }
      }

      override fun onServiceDisconnected(name: ComponentName?) {
        Log.d("CUSTOM_TAB", "Service Disconnected")
      }

      override fun onCustomTabsServiceConnected(name: ComponentName, client: CustomTabsClient) {
        customTabsSession = client.newSession(customTabsCallback)
        client.warmup(0L)
        val color = R.color.colorPrimaryDark
        val colorParams = CustomTabColorSchemeParams.Builder()
          .build()

        CustomTabsIntent.Builder(customTabsSession)
          .setShowTitle(false)
          .launchUrl(activity, location.toUri())
      }
    }
    CustomTabsClient.bindCustomTabsService(
      activity,
      "com.android.chrome",
      customTabsConnection!!
    )
    return Router.Decision.CANCEL
  }

MainActivity.nativateTo():

  fun navigateTo(destination: String) {
    navigator?.route(destination)
    selectBottomNavItemByUrl(destination)
  }

Log from Google Play Console:

Exception java.lang.NullPointerException:
  at com.myapp.handlers.CustomBrowserTabDecisionHandler$handle$1$customTabsCallback$1.onNavigationEvent (CustomBrowserTabDecisionHandler.kt:68)
  at androidx.browser.customtabs.CustomTabsClient$3$1.run (CustomTabsClient.java:493)
  at android.os.Handler.handleCallback (Handler.java:959)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loopOnce (Looper.java:249)
  at android.os.Looper.loop (Looper.java:337)
  at android.app.ActivityThread.main (ActivityThread.java:9511)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:636)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1005)

Users can close the custom tab and return to the app for various reasons, such as choosing another payment method.
Any help for solving this?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions