text
stringlengths
1
372
the first thing to do to use a FlutterFragment is to add it to a host
activity.
to add a FlutterFragment to a host activity, instantiate and
attach an instance of FlutterFragment in onCreate() within the
activity, or at another time that works for your app:
<code_start>
public class MyActivity extends FragmentActivity {
// define a tag string to represent the FlutterFragment within this
// activity's FragmentManager. this value can be whatever you'd like.
private static final string TAG_FLUTTER_FRAGMENT = "flutter_fragment";
// declare a local variable to reference the FlutterFragment so that you
// can forward calls to it later.
private FlutterFragment flutterFragment;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// inflate a layout that has a container for your FlutterFragment.
// for this example, assume that a FrameLayout exists with an ID of
// r.id.fragment_container.
setContentView(R.layout.my_activity_layout);
// get a reference to the activity's FragmentManager to add a new
// FlutterFragment, or find an existing one.
FragmentManager fragmentManager = getSupportFragmentManager();
// attempt to find an existing FlutterFragment,
// in case this is not the first time that onCreate() was run.
flutterFragment = (flutterfragment) fragmentManager
.findfragmentbytag(tag_flutter_fragment);
// create and attach a FlutterFragment if one does not exist.
if (flutterfragment == null) {
flutterFragment = FlutterFragment.createDefault();
fragmentManager
.begintransaction()
.add(
r.id.fragment_container,
flutterFragment,
TAG_FLUTTER_FRAGMENT
)
.commit();
}
}
}
<code_end>
<code_start>
class MyActivity : FragmentActivity() {
companion object {
// define a tag string to represent the FlutterFragment within this
// activity's FragmentManager. this value can be whatever you'd like.
private const val TAG_FLUTTER_FRAGMENT = "flutter_fragment"
}
// declare a local variable to reference the FlutterFragment so that you
// can forward calls to it later.
private var flutterFragment: FlutterFragment? = null
override fun onCreate(savedInstanceState: bundle?) {
super.onCreate(savedInstanceState)
// inflate a layout that has a container for your FlutterFragment. for
// this example, assume that a FrameLayout exists with an ID of
// r.id.fragment_container.
setContentView(R.layout.my_activity_layout)
// get a reference to the activity's FragmentManager to add a new
// FlutterFragment, or find an existing one.
val fragmentManager: FragmentManager = supportFragmentManager
// attempt to find an existing FlutterFragment, in case this is not the
// first time that onCreate() was run.
flutterFragment = fragmentManager
.findfragmentbytag(tag_flutter_fragment) as FlutterFragment?
// create and attach a FlutterFragment if one does not exist.
if (flutterfragment == null) {
var newFlutterFragment = FlutterFragment.createDefault()
flutterFragment = newFlutterFragment
fragmentManager
.begintransaction()
.add(
r.id.fragment_container,
newFlutterFragment,
TAG_FLUTTER_FRAGMENT
)
.commit()
}
}
}
<code_end>
the previous code is sufficient to render a flutter UI
that begins with a call to your main() dart entrypoint,
an initial flutter route of /, and a new FlutterEngine.
however, this code is not sufficient to achieve all expected
flutter behavior. flutter depends on various OS signals that
must be forwarded from your host activity to FlutterFragment.
these calls are shown in the following example:
<code_start>
public class MyActivity extends FragmentActivity {
@override
public void onPostResume() {
super.onPostResume();
flutterFragment.onPostResume();
}
@override
protected void onNewIntent(@NonNull intent intent) {
flutterFragment.onNewIntent(intent);
}
@override