简体   繁体   English

原生Android Activity启动二级路由后,如何让Flutter模块返回原生Android Activity?

[英]How to make Flutter module return to native Android activity after the native Android activity launches a secondary route?

I'm adding flutter to a existing Android project and everything is working fine, except one thing:我正在向现有的 Android 项目添加颤振,一切正常,除了一件事:

My Android project has only one Flutter module.我的 Android 项目只有一个 Flutter 模块。 In that Flutter module I have two routes that will be shown in different parts of the Android project:在那个 Flutter 模块中,我有两条路线将显示在 Android 项目的不同部分:

  initialRoute: '/',
  routes: {
    '/': (_) => MyHomePage(),
    '/settings': (_) => _SettingsPage()
  }

In the Android Activity if I start a new Flutter activity with this:在 Android Activity 中,如果我用这个启动一个新的 Flutter Activity:

startActivity(
            FlutterActivity
                .withCachedEngine(flutterEngineId)
                .build(this)
        )

It will start Flutter with the default page route ( / ) which is the expected result.它将使用默认页面路由 ( / ) 启动 Flutter,这是预期的结果。 Then in this Flutter page if I navigate back, it will pop the Flutter engine and navigate back to the Android Activity.然后在这个 Flutter 页面中,如果我导航回来,它会弹出 Flutter 引擎并导航回 Android Activity。 Everything good for now.现在一切都很好。

But if I start a Flutter activity with a custom initial route, and then I press the back button, it will not pop the Flutter engine and navigate to the Android activity but to the initial Flutter route (in this case the / route).但是,如果我使用自定义初始路由启动 Flutter 活动,然后按后退按钮,它不会弹出 Flutter 引擎并导航到 Android 活动,而是导航到初始 Flutter 路由(在本例中为/路由)。

    startActivity(
        FlutterActivity
            .withNewEngine()
            .initialRoute("/settings")
            .build(this)
    )

Why we're having this behavior if we are specifying the initial route while starting the FlutterActivity ?如果我们在启动FlutterActivity时指定初始路由,为什么会出现这种行为?

And if this is the expect behavior, then what is the best way of make it navigate to the Android activity?如果这是预期的行为,那么让它导航到 Android 活动的最佳方法是什么?

I'm adding flutter to a existing Android project and everything is working fine, except one thing:我在现有的Android项目中添加了Flutter,除了以下几点之外,一切工作正常:

My Andrid project have only one Flutter module in that Flutter module I have two routes that will be shown in different parts of the Android project:我的Andrid项目只有一个Flutter模块,在那Flutter模块中,我有两条路线将在Android项目的不同部分中显示:

  initialRoute: '/',
  routes: {
    '/': (_) => MyHomePage(),
    '/settings': (_) => _SettingsPage()
  }

In the Android Activity if I start a new Flutter activity with this:在Android活动中,如果我以此启动新的Flutter活动:

startActivity(
            FlutterActivity
                .withCachedEngine(flutterEngineId)
                .build(this)
        )

It will start Flutter with the default page route ( / ) wich is the expected result.它将以默认的页面路由( / )启动Flutter,这是预期的结果。 Then in this Flutter page if I navigate back, it will pop the Flutter engine and navigate back to the Android Activity.然后在该Flutter页面中,如果我向后导航,它将弹出Flutter引擎并导航回到Android Activity。 Everything good for now.现在一切都很好。

But if I start a Flutter activity with a custom initial route, and then I press the back button, it will not pop the Flutter engine and navigate to the Android activity but to the initial Flutter route (in this case the / route).但是,如果我使用自定义的初始路由启动Flutter活动,然后按后退按钮,它将不会弹出Flutter引擎并导航至Android活动,而是导航至初始Flutter路由(在本例中为/路由)。

    startActivity(
        FlutterActivity
            .withNewEngine()
            .initialRoute("/settings")
            .build(this)
    )

Why we're having this behaviour if we are specifying the initial route while starting the FlutterActivity ?如果在启动FlutterActivity时指定初始路线,为什么会有这种行为?

And if this is the expect behaviour, then what is the best way of make it navigate to the Android activity?如果这是预期的行为,那么使它导航到Android活动的最佳方法是什么?

Try SystemNavigator.pop() to pop the topmost flutter route尝试 SystemNavigator.pop() 弹出最顶层的 flutter 路由

More details at https://api.flutter.dev/flutter/services/SystemNavigator/pop.html更多详情请访问https://api.flutter.dev/flutter/services/SystemNavigator/pop.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM