text
stringlengths
1
474
as the main platform thread is rarely blocked.While a platform view is rendered with hybrid composition,
the Flutter UI is composed from the platform thread,
which competes with other tasks like handling OS or plugin messages.Prior to Android 10, hybrid composition copied each Flutter frame
out of the graphic memory into main memory, and then copied it back
to a GPU texture. As this copy happens per frame, the performance of
the entire Flutter UI might be impacted. In Android 10 or above, the
graphics memory is copied only once.Virtual display, on the other hand,
makes each pixel of the native view
flow through additional intermediate graphic buffers,
which cost graphic memory and drawing performance.For complex cases, there are some techniques that
can be used to mitigate these issues.For example, you could use a placeholder texture
while an animation is happening in Dart.
In other words, if an animation is slow while a
platform view is rendered,
then consider taking a screenshot of the
native view and rendering it as a texture.For more information, see:
<topic_end>
<topic_start>Restore state on Android
When a user runs a mobile app and then selects another
app to run, the first app is moved to the background,
or backgrounded. The operating system (both iOS and Android)
might kill the backgrounded app to release memory and
improve performance for the app running in the foreground.When the user selects the app again, bringing it
back to the foreground, the OS relaunches it.
But, unless you’ve set up a way to save the
state of the app before it was killed,
you’ve lost the state and the app starts from scratch.
The user has lost the continuity they expect,
which is clearly not ideal.
(Imagine filling out a lengthy form and being interrupted
by a phone call before clicking Submit.)So, how can you restore the state of the app so that
it looks like it did before it was sent to the
background?Flutter has a solution for this with the
RestorationManager (and related classes)
in the services library.
With the RestorationManager, the Flutter framework
provides the state data to the engine as the state
changes, so that the app is ready when the OS signals
that it’s about to kill the app, giving the app only
moments to prepare.Instance state vs long-lived state
When should you use the RestorationManager and
when should you save state to long term storage?
Instance state
(also called short-term or ephemeral state),
includes unsubmitted form field values, the currently
selected tab, and so on. On Android, this is
limited to 1 MB and, if the app exceeds this,
it crashes with a TransactionTooLargeException
error in the native code.<topic_end>
<topic_start>
Overview
You can enable state restoration with just a few tasks:Define a restorationId or a restorationScopeId
for all widgets that support it,
such as TextField and ScrollView.
This automatically enables built-in state restoration
for those widgets.For custom widgets,
you must decide what state you want to restore
and hold that state in a RestorableProperty.
(The Flutter API provides various subclasses for
different data types.)
Define those RestorableProperty widgets
in a State class that uses the RestorationMixin.
Register those widgets with the mixin in a
restoreState method.If you use any Navigator API (like push, pushNamed, and so on)
migrate to the API that has “restorable” in the name
(restorablePush, resstorablePushNamed, and so on)
to restore the navigation stack.Other considerations:Providing a restorationId to
MaterialApp, CupertinoApp, or WidgetsApp
automatically enables state restoration by
injecting a RootRestorationScope.
If you need to restore state above the app class,
inject a RootRestorationScope manually.The difference between a restorationId and
a restorationScopeId: Widgets that take a
restorationScopeID create a new restorationScope
(a new RestorationBucket) into which all children
store their state. A restorationId means the widget
(and its children) store the data in the surrounding bucket.<topic_end>
<topic_start>
Restoring navigation state
If you want your app to return to a particular route
that the user was most recently viewing
(the shopping cart, for example), then you must implement
restoration state for navigation, as well.If you use the Navigator API directly,
migrate the standard methods to restorable
methods (that have “restorable” in the name).
For example, replace push with restorablePush.The VeggieSeasons example (listed under “Other resources” below)
implements navigation with the go_router package.
Setting the restorationId
values occur in the lib/screens classes.<topic_end>
<topic_start>
Testing state restoration
To test state restoration, set up your mobile device so that
it doesn’t save state once an app is backgrounded.
To learn how to do this for both iOS and Android,
check out Testing state restoration on the
RestorationManager page.warning Warning
Don’t forget to reenable
storing state on your device once you are
finished with testing!<topic_end>
<topic_start>