我想告诉我的应用从MainActivity的onCreate方法内的确定路由开始,如下所示:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
    flutterView.setInitialRoute("anotherRoute") // <-- no result
}

但是它没有给出结果, Dart 侧面仍然接收标准的“/”作为window.defaultRouteName
如果我在调用super之前设置了它,它将为我提供NullPointer:
override fun onCreate(savedInstanceState: Bundle?) {
    flutterView.setInitialRoute("anotherRoute") // <-- NullPointer
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
}

最佳答案

您需要重写createFlutterView方法

  override fun createFlutterView(context: Context): FlutterView {
    val matchParent = WindowManager.LayoutParams(-1, -1)
    val view = FlutterView(this, null as AttributeSet?, this.createFlutterNativeView())
    view.setInitialRoute("/route2")
    view.layoutParams = matchParent

    this.setContentView(view)
    return view
  }

10-08 04:58