Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Adding a Flutter screen to an Android app' How to specify initialRoute with cache engine #48791

Closed
Vadaski opened this issue Jan 14, 2020 · 11 comments
Assignees
Labels
a: existing-apps Integration with existing apps via the add-to-app flow engine flutter/engine repository. See also e: labels. platform-android Android applications specifically
Milestone

Comments

@Vadaski
Copy link
Contributor

Vadaski commented Jan 14, 2020

Page URL: https://flutter.dev/docs/development/add-to-app/android/add-flutter-screen.html
Page source: https://github.com/flutter/website/tree/master/src/docs/development/add-to-app/android/add-flutter-screen.md

I read this article and found that when I use newEngine I can specify Flutter's initialRoute like this.

myButton.addOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    startActivity(
      FlutterActivity
        .withNewEngine()
        .initialRoute("/my_route")
        .build(currentActivity)
      );
  }
});

But when using cacheEngine, I didn’t find such methods. So can we use both cacheEngine and initialRoute?

@Vadaski
Copy link
Contributor Author

Vadaski commented Jan 14, 2020

Hi @xster
Can you help me look at this problem?

@zmtzawqlp
Copy link
Contributor

is there any way to initial route with cache engine? i'm working on add flutter into existing app.
so we should open a new flutter page with new engine everytime?

@xster
Copy link
Member

xster commented Jan 14, 2020

/move flutter/flutter

@xster xster transferred this issue from flutter/website Jan 14, 2020
@xster xster added a: existing-apps Integration with existing apps via the add-to-app flow d: website - content engine flutter/engine repository. See also e: labels. platform-android Android applications specifically labels Jan 14, 2020
@xster xster added this to Awaiting triage in Add-to-app - Android engine review via automation Jan 14, 2020
@xster xster added this to Awaiting triage in Add-to-app - documentation and examples review via automation Jan 14, 2020
@xster
Copy link
Member

xster commented Jan 14, 2020

@matthew-carroll can we add some clarifications to the website?

I know the API isn't ideal right now. Maybe we can just put a workaround in a tip box for now since whether we document it or not, this will be a recurring question.

@matthew-carroll
Copy link
Contributor

To answer the questions in this thread...

So can we use both cacheEngine and initialRoute?

When configuring a FlutterActivity or FlutterFragment to use a cached FlutterEngine, no, you cannot specify an initial route. An initial route only makes sense if it's configured before starting a FlutterEngine. A cached engine, by definition, is a FlutterEngine that is already started. Therefore, the concept of an initial route does not make sense.

If you have a requirement that is not covered by a new engine or a cached engine, please let us know what use-cases you're dealing with and we can see if it makes sense to extend the API.

so we should open a new flutter page with new engine everytime?

Not necessarily. If you want to configure an initial route using the API for FlutterActivity or FlutterFragment, then yes, you need to use a new FlutterEngine. However, you don't need to use those APIs to set an initial route. If you look at the instructions for creating a cached FlutterEngine here (https://flutter.dev/docs/development/add-to-app/android/add-flutter-screen#step-3-optional-use-a-cached-flutterengine) then just before you invoke executeDartEntrypoint() you can invoke:

flutterEngine.getNavigationChannel().setInitialRoute('my/route');

@xster I'll add instructions to the website guide for setting an initial route when creating a cached engine. I'm not sure where it would be appropriate to add comments in code - we can't really comment on a method that doesn't exist. Also, you mentioned the API isn't ideal. I think this part of the API makes sense. What kind of API do you think would be preferable?

@matthew-carroll matthew-carroll self-assigned this Jan 14, 2020
@matthew-carroll matthew-carroll added this to On Radar in Matt's WIP Jan 14, 2020
@zmtzawqlp
Copy link
Contributor

@matthew-carroll thanks for your reply, but i want to make things clear.
nativeA page=>flutterA page =>nativeB page=>flutterB page
is there any way that initial flutterA and flutterB with one cache engine? every new engine means more memory usage. This is what i want to konw , thanks.

@matthew-carroll
Copy link
Contributor

@zmtzawqlp you can definitely use the same FlutterEngine across subsequent pages as you described. But "initial route" will only set the very first route that a given FlutterEngine loads.

It sounds like what you want is to change the route of a FlutterEngine between pages. This is absolutely possible, but it currently requires that you implement that behavior yourself using method channels. The Flutter team intends to eventually provide built-in support for your use-case, but we're still collecting customer requirements to determine what that answer should look like.

In your case, you'll want to setup a method channel that does the following:

  • On the Java side, set up the channel so that you can send route names from Java to Dart
  • On the Dart side, whenever your channel receives a route name from Java, instruct your Navigator widget to switch to the given route.

Does that make sense?

@zmtzawqlp
Copy link
Contributor

@matthew-carroll yes, I got your point, FlutterA and FlutterB will in the same page stack, but it's difficult to control both native page stack and flutter page stack. Thank you very much for clarification, I'm look forward to that Flutter team will provide official way.

@Vadaski
Copy link
Contributor Author

Vadaski commented Jan 15, 2020

@zmtzawqlp you can definitely use the same FlutterEngine across subsequent pages as you described. But "initial route" will only set the very first route that a given FlutterEngine loads.

It sounds like what you want is to change the route of a FlutterEngine between pages. This is absolutely possible, but it currently requires that you implement that behavior yourself using method channels. The Flutter team intends to eventually provide built-in support for your use-case, but we're still collecting customer requirements to determine what that answer should look like.

In your case, you'll want to setup a method channel that does the following:

  • On the Java side, set up the channel so that you can send route names from Java to Dart
  • On the Dart side, whenever your channel receives a route name from Java, instruct your Navigator widget to switch to the given route.

Does that make sense?

Yes, It looks similar to the previous solution, Flutter Boost also uses this method, it should be feasible, thank you for your reply!

@matthew-carroll matthew-carroll moved this from On Radar to In progress in Matt's WIP Jan 16, 2020
@matthew-carroll matthew-carroll added this to the March 2020 milestone Jan 16, 2020
@matthew-carroll matthew-carroll added this to To do in Add-to-app 2020Q1 via automation Jan 17, 2020
@matthew-carroll matthew-carroll moved this from To do to In progress in Add-to-app 2020Q1 Jan 17, 2020
@matthew-carroll matthew-carroll moved this from Awaiting triage to Engineer reviewed in Add-to-app - Android engine review Jan 17, 2020
@matthew-carroll
Copy link
Contributor

Based on conversation in this thread, and @xster's comment near the top, I think the only actionable change is to add some info to Flutter's guides about using initial routes with cached engines. I added docs for that case here:

flutter/website#3551

The new initial route info can be found in the following guides:

https://flutter.dev/docs/development/add-to-app/android/add-flutter-screen

https://flutter.dev/docs/development/add-to-app/android/add-flutter-fragment

I'm going to close this issue.

Add-to-app 2020Q1 automation moved this from In progress to Done Jan 18, 2020
Add-to-app - documentation and examples review automation moved this from Awaiting triage to Engineer reviewed Jan 18, 2020
@matthew-carroll matthew-carroll moved this from In progress to Done in Matt's WIP Jan 18, 2020
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: existing-apps Integration with existing apps via the add-to-app flow engine flutter/engine repository. See also e: labels. platform-android Android applications specifically
Projects
Development

No branches or pull requests

4 participants