Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Android Embedding PR 11: Add FlutterEngine to FlutterFragment. #7972

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

package io.flutter.embedding.engine.android;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.view.FlutterMain;

/**
* {@code Fragment} which displays a Flutter UI that takes up all available {@code Fragment} space.
Expand Down Expand Up @@ -144,9 +149,62 @@ protected static Bundle createArgsBundle(@Nullable String dartEntrypoint,
return args;
}

@Nullable
private FlutterEngine flutterEngine;

public FlutterFragment() {
// Ensure that we at least have an empty Bundle of arguments so that we don't
// need to continually check for null arguments before grabbing one.
setArguments(new Bundle());
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

// When "retain instance" is true, the FlutterEngine will survive configuration
// changes. Therefore, we create a new one only if one does not already exist.
if (flutterEngine == null) {
createFlutterEngine();
}
}

/**
* Creates a new FlutterEngine instance.
*
* Subclasses can instantiate their own {@link FlutterEngine} by overriding
* {@link #onCreateFlutterEngine(Context)}.
*
* Subclasses can alter the {@link FlutterEngine} after creation by overriding
* {@link #onFlutterEngineCreated(FlutterEngine)}.
*/
private void createFlutterEngine() {
// Create a FlutterEngine to back our FlutterView.
flutterEngine = onCreateFlutterEngine(getActivity());

// Allow subclasses to customize FlutterEngine as desired.
onFlutterEngineCreated(flutterEngine);
}

/**
* Hook for subclasses to customize the creation of the {@code FlutterEngine}.
*
* By default, this method returns a standard {@link FlutterEngine} without any modification.
*/
@NonNull
protected FlutterEngine onCreateFlutterEngine(@NonNull Context context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of this seems awkward to me. I think this should just be called createFlutterEngine. Having a non-void method called onCreate seems unusual.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really a nit though, feel free to disregard if it fits some other pattern that I'm just not aware of.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention I was trying to follow here is similar to the onCreateView() method in Fragments. There's a primary method invoked in the superclass, in this case called createFlutterEngine(), which then internally invokes a subclass hook, in this case called onCreateFlutterEngine(). Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough

Log.d(TAG, "onCreateFlutterEngine()");
return new FlutterEngine(context);
}

/**
* Hook for subclasses to customize the {@link FlutterEngine} owned by this {@link FlutterFragment}
* after the {@link FlutterEngine} has been instantiated.
*
* Consider using this method to connect desired Flutter plugins to this {@code Fragment}'s
* {@link FlutterEngine}.
*/
protected void onFlutterEngineCreated(@NonNull FlutterEngine flutterEngine) {
// no-op
}
}