What is the Android Lifecycle? • Android apps have a “lifecycle” just like people do • The OS itself controls this lifecycle: example: You are using the Twitter app and then take an important phone call, the Twitter app disappears from the screen • Android decides when certain apps should shutdown when they are running in the background • As programmers we must be aware of this lifecycle The Different Phases • Android system has multiple distinct phases and these change how the app is used • It essentially uses this because phones are dependent on battery life and have less processing power than desktops • So it is important for the OS to “close” unused apps to prevent them from draining the battery • Each app can be in a different phase of it’s life: created, started, resumed, paused, stopped, destroyed The Different Phases II • When the user first starts an app it is “created” and the code inside onCreate runs • After it is “started” then it “resumes” and then it is considered “running” • This may seem strange but the first time we launch an app it still “starts’ ->”resumes” -> “runs” • Each of these phases have their own special methods like onCreate that can run The Different Phases in Reverse • Similarly, when an app closes or is shutdown we get different lifecycles • For example if we take a phone call while using the Twitter app we go through the pause and then the stop phase • If the app stays in the background long enough or gets closed by the user it enters the “destroy” phase The Android Lifecycle Why should we care? • The lifecycles of an Android app won’t play a huge role in this course but it’s still important to know • Imagine we’ve created an app for taking notes and the user is writing a long note • Then the user takes a phone call and your app goes into the background and they forget about it • When they return to the app, the user would be mad if the note they were writing disappeared too… Handling the Lifecycle • Each of the lifecycle events we’ve seen has a special method we can code in • For example, the onDestroy method will run when the app is about to be shut down • This would allow us to write code to save some last minute information • So we could write code that saves the note they were working on in a temp directory in case of sudden shutdown