Skip to content

Commit a7c6575

Browse files
RobertOdrowazcbrackenvashworth
authored andcommitted
iOS: Re-enable rendering on willEnterForeground (flutter#191350)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> This PR changes the moment the rendering is re-enabled after the app was in the background. Currently, the rendering is enabled only after the app enters the "active" state. This means that if something covers the app right after it returns from the background, e.g. a biometric prompt, the app visible under the prompt doesn't render new frames. This results in the app showing the frozen last frame from before going into background until the biometric prompt is dismissed. This PR changes the logic so that rendering is re-enabled when the app enters foreground, even if it's foreground inactive, so that the app content is updated normally. NOTE: It used to work this way until this PR flutter-team-archive/engine#7450, this was changed to match the rendering disabling logic that was moved to the `willResignActive` callback. The disabling was moved back to the`didEnterBackground` callback in this PR flutter-team-archive/engine#25196, so I believe the re-enabling can be safely moved back to `willEnterForeground`. flutter#191349 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Chris Bracken <[email protected]> Co-authored-by: Victoria Ashworth <[email protected]>
1 parent adc38eb commit a7c6575

3 files changed

Lines changed: 213 additions & 45 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm‎

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -661,19 +661,37 @@ - (UISceneActivationState)activationState {
661661
- (BOOL)stateIsActive {
662662
// [UIApplication sharedApplication API is not available for app extension.
663663
UIApplication* flutterApplication = FlutterSharedApplication.application;
664-
BOOL isActive = flutterApplication
665-
? [self isApplicationStateMatching:UIApplicationStateActive
666-
withApplication:flutterApplication]
667-
: [self isSceneStateMatching:UISceneActivationStateForegroundActive];
668-
return isActive;
664+
665+
if (flutterApplication) {
666+
return [self isApplicationStateMatching:UIApplicationStateActive
667+
withApplication:flutterApplication];
668+
}
669+
return [self isSceneStateMatching:UISceneActivationStateForegroundActive];
670+
}
671+
672+
- (BOOL)stateIsForeground {
673+
// [UIApplication sharedApplication API is not available for app extension.
674+
UIApplication* flutterApplication = FlutterSharedApplication.application;
675+
676+
if (flutterApplication) {
677+
return [self isApplicationStateMatching:UIApplicationStateActive
678+
withApplication:flutterApplication] ||
679+
[self isApplicationStateMatching:UIApplicationStateInactive
680+
withApplication:flutterApplication];
681+
}
682+
return [self isSceneStateMatching:UISceneActivationStateForegroundActive] ||
683+
[self isSceneStateMatching:UISceneActivationStateForegroundInactive];
669684
}
670685

671686
- (BOOL)stateIsBackground {
672687
// [UIApplication sharedApplication API is not available for app extension.
673688
UIApplication* flutterApplication = FlutterSharedApplication.application;
674-
return flutterApplication ? [self isApplicationStateMatching:UIApplicationStateBackground
675-
withApplication:flutterApplication]
676-
: [self isSceneStateMatching:UISceneActivationStateBackground];
689+
690+
if (flutterApplication) {
691+
return [self isApplicationStateMatching:UIApplicationStateBackground
692+
withApplication:flutterApplication];
693+
}
694+
return [self isSceneStateMatching:UISceneActivationStateBackground];
677695
}
678696

679697
- (BOOL)shouldHandleSceneNotification:(NSNotification*)notification API_AVAILABLE(ios(13.0)) {
@@ -729,6 +747,18 @@ - (void)surfaceUpdated:(BOOL)appeared {
729747
}
730748
}
731749

750+
// Creates the surface if the viewport is sized.
751+
//
752+
// Callers must guarantee GPU access is permitted since UIApplication reports
753+
// UIApplicationStateBackground while willEnterForeground is delivered, so the application state
754+
// can't be enforced here.
755+
- (void)createSurfaceIfReady {
756+
if (!self.engine || !_viewportMetrics.physical_width) {
757+
return;
758+
}
759+
[self surfaceUpdated:YES];
760+
}
761+
732762
#pragma mark - UIViewController lifecycle notifications
733763

734764
- (void)viewDidLoad {
@@ -815,9 +845,7 @@ - (void)viewWillAppear:(BOOL)animated {
815845

816846
// Only recreate surface on subsequent appearances when viewport metrics are known.
817847
// First time surface creation is done on viewDidLayoutSubviews.
818-
if (_viewportMetrics.physical_width) {
819-
[self surfaceUpdated:YES];
820-
}
848+
[self createSurfaceIfReady];
821849
[self.engine.lifecycleChannel sendMessage:@"AppLifecycleState.inactive"];
822850
[self.engine.restorationPlugin markRestorationComplete];
823851
}
@@ -1021,9 +1049,6 @@ - (void)sceneWillEnterForeground:(NSNotification*)notification API_AVAILABLE(ios
10211049

10221050
- (void)appOrSceneBecameActive {
10231051
self.keyboardInsetManager.isKeyboardInOrTransitioningFromBackground = NO;
1024-
if (_viewportMetrics.physical_width) {
1025-
[self surfaceUpdated:YES];
1026-
}
10271052
[self performSelector:@selector(goToApplicationLifecycle:)
10281053
withObject:@"AppLifecycleState.resumed"
10291054
afterDelay:0.0f];
@@ -1048,6 +1073,7 @@ - (void)appOrSceneDidEnterBackground {
10481073
}
10491074

10501075
- (void)appOrSceneWillEnterForeground {
1076+
[self createSurfaceIfReady];
10511077
[self goToApplicationLifecycle:@"AppLifecycleState.inactive"];
10521078
}
10531079

@@ -1319,13 +1345,13 @@ - (void)viewDidLayoutSubviews {
13191345
[self setViewportMetricsPaddings];
13201346
[self updateViewportMetricsIfNeeded];
13211347

1322-
// There is no guarantee that UIKit will layout subviews when the application/scene is active.
1323-
// Creating the surface when inactive will cause GPU accesses from the background, so only
1324-
// create the surface when the application/scene is actually active.
1348+
// There is no guarantee that UIKit will layout subviews when the application/scene is in the
1349+
// foreground. Creating the surface when backgrounded will cause app termination, so in that
1350+
// scenario, skip it here, and leave surface creation to appOrSceneWillEnterForeground.
13251351
// This must run after updateViewportMetrics so that the surface creation tasks are queued after
13261352
// the viewport metrics update tasks.
1327-
if (firstViewBoundsUpdate && self.stateIsActive && self.engine) {
1328-
[self surfaceUpdated:YES];
1353+
if (firstViewBoundsUpdate && self.stateIsForeground) {
1354+
[self createSurfaceIfReady];
13291355
}
13301356
}
13311357

0 commit comments

Comments
 (0)