(this is a mini tutorial that can be completed at any time)
The aim of this tutorial is to look at how the activity lifecycle works which looks like this:
for more information take a look at https://developer.android.com/guide/components/activities/activity-lifecycle.html There’s also a really great video by Kristin Marsicano https://realm.io/news/activities-in-the-wild-exploring-the-activity-lifecycle-android/
Firstly, we’re going to implement the different activity lifecycle methods. Android Studio has a lot of keyboard shortcuts which will write a lot of the code for us! On Mac OS Z press
Command + N and on Windows press
Alt + Insert . Here you can select
Override methods and find the methods listed above.
console.log . These we can check in the Android Monitor (probably a button at the bottom of your window in Android Studio) which methods are being called.
When you first look in the Android Monitor, you’ll notice a lot of messages coming through. We can filter out which ones we care about by using a tag, like so:
As you write a log message inside each method, speak to your coach about when you think this might get called!
this is what my code looks like
Run this on a real device (or an emulator) and test it out and answer with your coach the following questions:
a. What methods get called when the app first starts up? b. What methods get called if I go home and come back into the app? c. What methods get called if I kill the app, and then open it again?
Which methods get called if you rotate your device?
Is that what you expected?
What other things could you do to your device that might destroy and re-create your activity? (Hint: changing the language)
Speak to your coach about your coach and see where they’ve learned the hard way about the activity lifecycle! Has it ever caused an app they’ve written to crash, or behave in a way they didn’t intend for?
4. Pop quiz
For the following questions, do you think they are true or false?
- After onCreate, the activity is now visible to the user to interact with.
- onStart always gets called every time the user comes back into the activity.
- onResume always gets called when you return to the activity, regardless of whether you killed the activity or just briefly left it.
- an activity in onPause can still be visible on the screen.
- onDestroy is the best place to save anything a user did so they can see it when they come back.
ǝnɹʇ ˙ʇɐƃnou uı sddɐ pǝʍopuıʍ ıʇןnɯ ‘ǝnɹʇ 4 ˙ǝnɹʇ 3 ˙ǝsןɐɟ 2 ˙ǝɯnsǝɹuo ɹǝʇɟɐ s’ʇı ‘ǝsןɐɟ 1