 Hey everybody it's Brian and welcome to the 38th Flutter tutorial. Sorry my phone made a weird noise. I had some user feedback. I really tried to keep up on my inbox but sometimes it's just impossible. This one really stood out. It said hey I've been doing work in Android and I want to know how to do application lifecycle in Flutter. If you have no idea what I'm talking about just Google Android app lifecycle. And there's tons of graphics out here that kind of explain there's a created, a started, a resume, a pause, a stopped. And what is all this? What does it really mean? All right so if we just do our live template we're going to run this. Here you see we have the clock application open. It's going to flip over into our app. So this thing is now running. Now what happens if we go back into that? Well cell phones are not like your laptop sitting in front of your desktop. They run on limited power. They have batteries and they have power constraints. So what they do is when you put an app into the background they will pause that app and when you pull it into the foreground they'll resume that app. So sometimes some applications it's very helpful to know when the application is in and out of the foreground or when it's paused or suspended. So that's what we're really going to look at today. Kind of non-visual. You're going to have to kind of take a little bit of a leap of faith here but that's what we're going to do. All right so the first thing we're going to do is we are going to say with widgets binding observer bang. So what this will do is it'll allow us to actually observe the life cycle in pretty much real time. So we're going to do a control o and just have a look around here. You notice how there is some some interesting properties here like uh did change locale, did change application lifestyle state. Ooh that sounds promising. We're definitely going to add that and then we're going to just control o again and we want the init state and let's just control o one more time and we're just going to have a little peak here. Dispose. There it is. So now what you can see is kind of a pattern here. We got when the state is initialized or when it's first turned on when the did change app life cycle state meaning when the cycle changed. Did it get paused, resumed, suspended and then when it's disposed meaning it's no longer needed. Remember in Dart there is no such concept as a deconstructor so you can't simply say hey the deconstructor of this class like you would with other languages you have to actually track this whole life cycle. It gets a little tricky but it's really not too hard so we're going to say print init state, super dot init state and then widgets, oops widgets binding observer the instance why are you not doing oh that's why widgets binding instance dot add observer and we're just going to add this the current object here. So what we're doing there is we're binding to the widgets binding instance blah blah blah basically saying we want to observe this object's life cycle. Then in the dispose we'll fill this out because it's pretty easy we're just going to do the exact opposite here and actually pull this up here so we can see it a little better and we're going to say we're going to say dispose and we're going to say super dot dispose but we have to do this at the end here and here we're going to remove observer so we're going to stop watching this object. Now the magic actually happens right here in this did change app life cycle state and what we need to do is just say print and we're going to just say we're just going to print out the current state and then we want to actually switch on this thing let's say switch state and we're going to say case app life cycle state dot and let's say inactive paused why is this suddenly griping and complaining missing oh that's why it knows what we need so it wants to force us to do everything so we're just going to add those in super quick we're going to say paused we want resumed and suspending and then we're just going to grab that because I want those to stand out a little bit here we'll say inactive paused resumed and of course suspending all right let's actually reload that whole app and you can see the first thing that happened was in its state move where is it do a full restart on this thing so that restarted hmm did I break it I have a very good knack of breaking things we're just going to clear everything out of here it started again maybe that's a bug in flutter I don't know all right so we're going to start this whole thing again disclaimer alert flutter is still an alpha but I have a habit of just horribly breaking things so okay so there is our init state right here bang and what we're going to do is we're going to actually and you see as soon as we click that it's still kind of there it's in the app chooser you can see how the state equal lifecycle paused and if we go back resumed so that's kind of what it would do so if we were to let's just clear this out where's my dart analysis is not working darn it I gotta figure out why that doesn't work but anyways so if we go in here and we just go in here and we open like say the clock you can see how our app is paused and then if we just go back in you can see how the state equals resumed and resumed and life goes on now let's clear this out and let's actually kill our app and see what happens here and you can see how lost connection to device but nothing actually happened we didn't actually get to dispose there kind of a little disappointed in there and it might be a bug in my code not necessarily in flutter itself but it gives you a rough idea of how to actually interact with these now what would you use them for well let's say you're running like a live video feed or something well whenever it gets paused you want to actually pause that video feed you want to stop downloading because the user is no longer looking at it and you're just chewing up their battery and then when it resumes when they pull it back into the foreground that's when you would start it again and it and dispose are a little bit different and it is where you would like load the settings off the disk or download from firebase where dispose would be where you would clean everything up and i'm kind of a little wondering why i never got to dispose here i'll have to figure that out but uh i hope that answered your question i know it took me a while to figure this out and there really wasn't a whole lot of clear examples out on the web that i found maybe you'll have better luck but at any rate thank you for watching i hope you found this educational and entertaining um source code for this obviously will be out on voidrumbs.com you just go to tutorials and then github and be sure to join the voidrumbs facebook group