positron
positron

Reputation: 3693

Launching specific Flutter screen

I have an app with 3 screens. I also have platform specific code where html files are being handled by my app. What I am looking to achieve is every time user opens an html file, it opens in my app, and specific page is opened, let's say 2. I have code to handle opening files in the app (when there is only one screen), but I am not sure how to handle multiple screens.

This relevant code:

   @Override
  public void configureFlutterEngine(FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
    new MethodChannel(getFlutterEngine().getDartExecutor(), CHANNEL).setMethodCallHandler(new MethodCallHandler() {
      @Override
      public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        if (call.method.equals("getReportFile")) {
          try {
            String res = getReportFile();
            result.success(res);
          } catch (SameFilesException e) {
            Log.i(TAG, e.getLocalizedMessage());
          }
        }
      }
    });
  }

Ideally I'd like to be able to set specific screen from MainActivity, as it would play nicely in MVC pattern, but I am not sure how to go about doing it. There is mention about https://api.flutter.dev/javadoc/io/flutter/embedding/android/FlutterActivity.html#getInitialRoute--, but I am not certain where can I use method 1: Pass a boolean as FlutterActivityLaunchConfigs.EXTRA_INITIAL_ROUTE with the launching Intent, during the execution of MainActivity.

Upvotes: 2

Views: 3425

Answers (2)

Igor Kharakhordin
Igor Kharakhordin

Reputation: 9873

You could extend io.flutter.embedding.android.FlutterActivity to override initial route or dart entry point. It's also possible to push and pop routes:

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    @NonNull
    @Override
    public String getDartEntrypointFunctionName() {
        return "anotherMain";
    }

    @NonNull
    @Override
    public String getInitialRoute() {
        String action = getIntent().getAction();

        // Initial route depends on intent's action
        if (action != null && action.equals("example_action")) {
            return "some_route";
        } else {
            return "another_route";
        }
    }

    @Override
    protected void onNewIntent(@NonNull Intent intent) {
        String action = intent.getAction();

        // Example action
        boolean routeIntent = action != null && action.equals("push_a_route");
        FlutterEngine engine = getFlutterEngine();

        if (routeIntent && engine != null) {
            // Pushing a new route when new intent received
            engine.getNavigationChannel().pushRoute("some_route");
        }
    }

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        GeneratedPluginRegistrant.registerWith(new ShimPluginRegistry(flutterEngine));
    }
}

Upvotes: 6

Diefferson Santos
Diefferson Santos

Reputation: 77

The FlutterActivity has a method called getInitialRoute() that return a string with initial route, the default value is "/" but you can override this method for this activity start in another route.

Upvotes: 3

Related Questions