A native resource should live as long as the responsibility that needs it. Keeping every camera session, listener, SDK instance, or file handle alive for the whole app can waste resources and create callbacks into features that no longer exist.
Define ownership before adding cleanup code. A screen, a shared service, and a background operation can legitimately have different lifetimes.
Name the owner and release conditions
For each resource, record who acquires it, who can share it, which operations depend on it, and what events end its lifetime. Include success, failure, cancellation, sign-out, engine detach, and native lifecycle transitions where relevant.
Review the native API and Flutter's current plugin development guidance. On Android, the ActivityAware reference explains lifecycle boundaries for activity-related bindings; apply only the model relevant to the implementation.
Test a fictional recording screen
Suppose opening a voice-note screen starts a native recording resource. Leaving the screen should follow a defined policy: stop and preserve a draft, cancel and remove temporary data, or continue through a separately owned service.
Navigate in and out repeatedly, cancel, trigger a native error, change accounts, background the app, and destroy the relevant native owner in a controlled test. Verify listeners, sessions, temporary files, and pending callbacks are released or transferred deliberately.
Count or inspect resource state using supported diagnostics rather than claiming a leak from one visual symptom. No memory measurement or successful cleanup test is claimed in this proposed exercise.
Handle completion after the owner ends
A late callback can arrive after cancellation or teardown. Give operations stable identity and a rule for discarding or reconciling late results. Avoid updating a newly created screen through an old callback reference.
Shared resources need explicit reference or ownership rules so one feature does not close a session still needed by another. Conversely, a generic singleton should not become an excuse for indefinite lifetime and hidden account state.
The tradeoff is reuse versus lifecycle complexity. Reinitialising a costly SDK for every small action may be inefficient, while retaining it indefinitely may waste resources or preserve stale credentials. Measure and choose the narrowest lifetime that fulfils the workflow.
The Social App case study provides related native and mobile context. This recording screen is fictional and does not report a leak or cleanup implementation there.
Questions about native resource lifetime
Should every resource be a singleton?
No. Use lifetime based on responsibility and actual reuse. Long-lived objects need explicit account, lifecycle, and cleanup rules.
Is widget disposal enough?
Only if it correctly connects to the native resource's ownership and supported release path. Review native callbacks and in-flight work too.
What should be tested first?
Open, use, cancel, and reopen the feature repeatedly, then inspect native resources and late callbacks. Bring findings to a native-lifecycle review.