跪拜 Guibai
← All articles
Frontend · Android · Flutter

Flutter's Default Activity Now Hosts Jetpack Compose: ViewTree Owners Land for Lifecycle, SavedState, and ViewModel

By 恋猫de小郭 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

ComposeView now works inside Flutter's AndroidView without manual owner plumbing, and the Virtual Display fix covers split-screen and multi-window paths. Reusing a cached FlutterEngine won't leave ComposeUI bound to a destroyed Activity.

Summary

Flutter's newest PR (#195523) adds Jetpack ComposeView support to the default FlutterActivity. The change makes FlutterActivity implement SavedStateRegistryOwner and ViewModelStoreOwner, and publishes a LifecycleOwner onto the DecorView. That lets a ComposeView created inside an AndroidView walk up the view tree and find the AndroidX hosts it needs for lifecycle, state restoration, and ViewModel scoping.

Previously this only worked when you used FlutterFragmentActivity or manually injected owners, because the plain FlutterActivity never wired those interfaces into the view hierarchy. The PR also handles Virtual Display: ComposeView inside a SingleViewPresentation lives in a separate window with no common parent to the Activity's DecorView, so a new ViewTreeOwners helper reads the owners from the current FlutterView and installs them on the Presentation's root view. Owner references are re-fetched on recreation to avoid stale lifecycle from a reused FlutterEngine.

Takeaways
— PR #195523 makes FlutterActivity implement SavedStateRegistryOwner and ViewModelStoreOwner and publish LifecycleOwner to DecorView.
— ComposeView requires LifecycleOwner, SavedStateRegistryOwner, and ViewModelStoreOwner in the view tree.
— Previously FlutterActivity lacked SavedStateRegistry and ViewModelStore; FlutterFragmentActivity worked around it.
— SavedStateRegistry restores before ON_CREATE, saves in onSaveInstanceState; ViewModelStore reuses via onRetainNonConfigureInstance.
— Virtual Display uses SingleViewPresentation with its own window; new ViewTreeOwners installs owners from FlutterView onto Presentation root.
— Owners are re-fetched on Presentation creation and recreation to avoid stale lifecycle from engine reuse.
Conclusions

The Virtual Display complication is the real blocker, not the primary Activity patch.

Re-fetching owners on recreation directly addresses FlutterEngine reuse, showing the team anticipates detach and reattach flows.

This is plumbing work that makes ComposeView a first-class citizen in Flutter's existing AndroidView mechanism without new API surface.

Concepts & terms
ViewTreeOwners
A helper in the PR that finds the LifecycleOwner, SavedStateRegistryOwner, and ViewModelStoreOwner from the current FlutterView and installs them on a Presentation's root view, so ComposeView in Virtual Display can locate them.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗