Closed
Labels
P3Issues that are less important to the Flutter projectIssues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityengineflutter/engine related. See also e: labels.flutter/engine related. See also e: labels.frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.platform-iosiOS applications specificallyiOS applications specifically
Description
I have the following code which will replace the child with a container when the app goes to inactive or paused state.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: SecureScreen(
child: Container(
color: Colors.red,
),
),
),
);
}
}
class SecureScreen extends StatefulWidget {
final Widget child;
SecureScreen({@required this.child});
@override
SecureScreenState createState() => SecureScreenState();
}
class SecureScreenState extends State<SecureScreen>
with WidgetsBindingObserver {
AppLifecycleState _notification;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
print(state.toString());
setState(() {
_notification = state;
});
}
@override
Widget build(BuildContext context) {
return (_notification == AppLifecycleState.paused ||
_notification == AppLifecycleState.inactive)
? Container(
color: Colors.greenAccent,
)
: widget.child;
}
}
So when I double click the home button in IOS to view all the open applications, it is triggering
"inactive" state but it is not calling the build() method to render container with greenAccent color.
I have attached the screenshot for you reference.
Reacted by djkloop, Heikki Uusitalo, Erik Madsen, Venkateswaran Mahadevan, vplme, braj065, Tomonori Ueno, Kasun Vithanage, Zhuoran John Li, mono — Masayuki Ono and 10 more
Metadata
Metadata
Assignees
Labels
P3Issues that are less important to the Flutter projectIssues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityengineflutter/engine related. See also e: labels.flutter/engine related. See also e: labels.frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.platform-iosiOS applications specificallyiOS applications specifically

Activity
[-]Render widgets based on AppLifeCycleState, Hide the page when viewing list of running apps[/-][+]Render widgets based on AppLifeCycleState is not working (build method is never called for inactive state)[/+]pravinarr commentedon Jun 18, 2019
Related #27344 #28936
dnfield commentedon Jun 19, 2019
I suspect what we want here is something in
viewWillDisappeargetting pumped down to the framework somehow. I don't think we can reliably expect anything to execute in a paused/inactive state for drawing on the screen.dnfield commentedon Jun 19, 2019
Per input from @matthew-carroll marking this as critical. Customer(s) who want to hide what's on the screen before going to background need this.
matthew-carroll commentedon Jun 19, 2019
Triage note: This issue prevents security conscious apps from clearing sensitive UI from apps that are moved to background.
dnfield commentedon Jun 19, 2019
I'm not sure that there's goin got be a great way to do this in Dart - particularly in a way that would guarantee that a message arrived in Dart land in time for Flutter to render a new frame with the desired content.
However, you can do this pretty trivially in a Flutter app. On iOS, following https://developer.apple.com/library/archive/qa/qa1838/_index.html, I changed up
AppDelegate.min a sample project to be like this:And now I get a black screen in the switcher.
pravinarr commentedon Jun 20, 2019
@dnfield Thank you for providing workaround for this issue. It worked perfectly. Can you provide some guidance to achieve the same on android?
dnfield commentedon Jun 20, 2019
You can add
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);toonCreateofMainActivity.java, which will also end up preventing screen shots from working. If you want it only to work when the app gets backgrounded, you could instead do something likeAlthough I'm not 100% sure this would end up covering something like an embedded platform view (e.g. a webview in your flutter app).
dnfield commentedon Jun 20, 2019
Based on the work arounds available for this, I'm going to remove the critical label. Please let me know (or feel free to re-add it if you're able) if this needs more attention again.
In case it's not already clear, I think it's unlikely that we'll be able to do a whole lot here on the framework side for this, but perhaps there's some good way to turn this into a plugin of some sort.
22 remaining items
gabdsg commentedon May 13, 2020
Any updates?
duck-dev-go commentedon Jul 10, 2020
Any updates?
muarachmann commentedon Sep 28, 2020
The Flutter Application doesn't override methods like
onResume,onPause, is there anyway to by pass this? Also tried gettinggetCurrentActivity().onPause(....)but not working says its protected. I will appreciate any help thanksmuarachmann commentedon Sep 28, 2020
Used
Application.ActivityLifecycleCallbacksandregisterActivityLifecycleCallbacks(listener)then I could access theonActivityResumed()andonActivityPaused()Jethro87 commentedon Oct 21, 2020
@audente Would you mind posting your Kotlin code from #33236 (comment)? Your swift code was super helpful, thanks 👍
Jethro87 commentedon Oct 21, 2020
Managed to get it working. Reference for those that come across this issue:
Note that this is using
FlutterFragmentActivityinstead ofFlutterActivity. Cheers.AdeolaKOD commentedon Nov 15, 2020
Thanks for this. I was able to get it working.
Just to clarify, on IOS the result of the swift code from @audente and @dnfield , will only be able to show us a blank screen when switching apps ?
gaaclarke commentedon Mar 29, 2021
Fixed in flutter-team-archive/engine#25196
fribeau commentedon Apr 10, 2021
It seems that the same issue is also happening on Android.
I just copied-pasted the same exact code shown at the beginning of this issue (minus some question marks in order to work with null safety) and I have the same unwanted behavior: the background stays always
redno matter the state.Does the fixed made 12 days ago only works for iOS or should I try another approche than
WidgetsBindingObserverto reach the same goal ?This is the output of my
flutter doctor(I work with a Pixel 5):gaaclarke commentedon Apr 12, 2021
@fribeau can you file another issue for android, please? If the steps are the same, just copy and paste them and link this issue.
The fix will only work for iOS, there isn't a cross platform solution since it's tightly tied to the embedder.
github-actions commentedon Aug 3, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.