Android FragmentManager has not been attached to a host

1.4k views Asked by At

I need to create a Flutter plugin and link native Android code from ADA bot. I have no experience with android but by following the documentation, it should be simple:

val adaView = AdaEmbedView(getContext())
val adaSettings = AdaEmbedView.Settings.Builder("ada-example").build()
adaView.initialize(adaSettings)

val dialog = AdaEmbedDialog()
adaDialog.arguments = Bundle().apply { 
    putParcelable(AdaEmbedDialog.ARGUMENT_SETTINGS, settings) 
}
dialog.show(supportFragmentManager, AdaEmbedDialog.TAG)

When I run this code I get the error:

Failed to handle method call java.lang.IllegalStateException: FragmentManager has not been attached to a host.

I write the code in the flutter create plugin autogenerated file FlutterAdaPlugin.kt and it looks like this:

public class FlutterAdaPlugin: FlutterPlugin, ActivityAware, MethodCallHandler, PluginRegistry.ActivityResultListener, FragmentActivity() {
  private lateinit var methodChannel : MethodChannel
  private lateinit var eventChannel : EventChannel
  private lateinit var eventSink : EventChannel.EventSink
  private lateinit var adaFramerwork: AdaEmbedView
  private lateinit var applicationCnt: Context
  private var activity: Activity? = null
  private lateinit var addaSettings: AdaEmbedView.Settings

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) : Boolean {
    super.onActivityResult(requestCode, resultCode, data)
    return true
  }

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    onAttachedToEngine(flutterPluginBinding.applicationContext, flutterPluginBinding.binaryMessenger)
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    this.applicationCnt = applicationContext
  }


  private fun onAttachedToEngine(applicationContext: Context, messenger: BinaryMessenger) {
    this.applicationCnt = applicationContext
    methodChannel = MethodChannel(messenger, "FlutterAdaPlugin")
    eventChannel = EventChannel(messenger, "FlutterAdaPluginStream")

    methodChannel.setMethodCallHandler(this)
    eventChannel.setStreamHandler(object : EventChannel.StreamHandler {

      override fun onListen(listener: Any?, eventSink: EventChannel.EventSink) {
        [email protected] = eventSink;
      }
    })
  }


  companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
      val plugin = FlutterAdaPlugin()
      plugin.activity = registrar.activity()
      registrar.addActivityResultListener(plugin)
      plugin.onAttachedToEngine(registrar.context(), registrar.messenger())
    }
  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    when (call.method) {
      "launchNavWebSupport" -> launchNavWebSupport()
      }
      else -> result.notImplemented()
    }
  }

  private fun launchNavWebSupport() {
    setupAdaFramework()

    val dialog = AdaEmbedDialog()
    dialog.arguments = Bundle().apply {
      putParcelable(AdaEmbedDialog.ARGUMENT_SETTINGS, addaSettings)
    }
    if(supportFragmentManager != null) {
    }
    dialog.show(supportFragmentManager, AdaEmbedDialog.TAG)
  }

  private fun setupAdaFramework() {
    val activity: Context = activity?.takeIf { activity != null } ?: return
    adaFramerwork = AdaEmbedView(activity)
     addaSettings = AdaEmbedView.Settings.Builder("test")
            .build()
    adaFramerwork.initialize(addaSettings)
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    eventChannel.setStreamHandler(null)
    methodChannel.setMethodCallHandler(null)
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activity = binding.activity
    binding.addActivityResultListener(this)
  }

  override fun onDetachedFromActivity() {
    activity = null
  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    activity = binding.activity
    binding.addActivityResultListener(this)
  }

  override fun onDetachedFromActivityForConfigChanges() {
    activity = null
  }
}
0

There are 0 answers