Reset FlutterFragment content using Flutter Engine while using FlutterFragment
Asked Answered
S

1

9

I am using a Flutter module inside my Android Application. I have defined a global Flutter Engine in a pre heated state in my Activity and is attaching it to a FlutterFragment as and when required in a particular Scenario. Below is pre heated engine called once the onCreate()on Activity is triggered.

void setupFlutterEngine(){
        FlutterEngine flutterEngine=new FlutterEngine(getApplicationContext());

//        Start executing Dart code in the FlutterEngine.
        flutterEngine.getDartExecutor().executeDartEntrypoint(
                DartExecutor.DartEntrypoint.createDefault()
        );

//      Cache the pre-warmed FlutterEngine to be used later by FlutterFragment.
        FlutterEngineCache
                .getInstance()
                .put(FLUTTER_ENGINE_CACHE_TAG, flutterEngine);

    }

Everything is working fine, I am instantiating and adding the cached engine to my FlutterFragment as below :

FlutterEngine flutterEngine=FlutterEngineCache.getInstance().get(FLUTTER_ENGINE_CACHE_TAG);

flutterMethodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), methodChannelTag);

flutterMethodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                
            }
        });

        GeneratedPluginRegistrant.registerWith(flutterEngine); 

flutterFragment = FlutterFragment
                .withCachedEngine(FLUTTER_ENGINE_CACHE_TAG)
                .renderMode(RenderMode.surface)
                .transparencyMode(TransparencyMode.opaque)
                .shouldAttachEngineToActivity(true)
                .build();

I want to finish the current view of flutter module when the fragment is destroyed but do not want to destroy the engine, I would like to reuse the FlutterEngine and want to re-start from the very first screen under main() method in Flutter module when the fragment is again added.

Scaramouch answered 11/7, 2020 at 22:4 Comment(2)
Hi, any solutions of this? I encountered the same requirement here.Distilled
Hi Nitant, Did you find any solution of this ?Gompers
G
0

if your issue like this one https://github.com/flutter/flutter/issues/65829

i find a solution for this problem, just override this method on FlutterActivity:

    @Override
    public void onFlutterUiNoLongerDisplayed() {
    super.onFlutterUiNoLongerDisplayed();
    if(isFinishing()){
        MethodChannel channel = new MethodChannel(cachedEngine.getDartExecutor().getBinaryMessenger(), "your_plugin");
        channel.invokeMethod("pop", null);
    }
}

and from your flutter module pop the route .

Galliot answered 9/6 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.