-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathactivity-lifecycle.html
More file actions
97 lines (90 loc) · 13.9 KB
/
Copy pathactivity-lifecycle.html
File metadata and controls
97 lines (90 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>Activity Lifecycle · codebar android tutorials</title><meta name="viewport" content="width=device-width"/><meta name="generator" content="Docusaurus"/><meta name="description" content="#Android Dev - Activity Lifecycle"/><meta name="docsearch:language" content="en"/><meta property="og:title" content="Activity Lifecycle · codebar android tutorials"/><meta property="og:type" content="website"/><meta property="og:url" content="https://codebar.github.io/android-tutorials/"/><meta property="og:description" content="#Android Dev - Activity Lifecycle"/><meta property="og:image" content="https://codebar.github.io/android-tutorials/img/undraw_online.svg"/><meta name="twitter:card" content="summary"/><meta name="twitter:image" content="https://codebar.github.io/android-tutorials/img/undraw_tweetstorm.svg"/><link rel="shortcut icon" href="/android-tutorials/img/favicon.ico"/><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"/><link rel="alternate" type="application/atom+xml" href="https://codebar.github.io/android-tutorials/blog/atom.xml" title="codebar android tutorials Blog ATOM Feed"/><link rel="alternate" type="application/rss+xml" href="https://codebar.github.io/android-tutorials/blog/feed.xml" title="codebar android tutorials Blog RSS Feed"/><script type="text/javascript" src="https://buttons.github.io/buttons.js"></script><script src="/android-tutorials/js/scrollSpy.js"></script><link rel="stylesheet" href="/android-tutorials/css/main.css"/><script src="/android-tutorials/js/codetabs.js"></script></head><body class="sideNavVisible separateOnPageNav"><div class="fixedHeaderContainer"><div class="headerWrapper wrapper"><header><a href="/android-tutorials/"><img class="logo" src="/android-tutorials/img/codebar-full-white-logo.png" alt="codebar android tutorials"/><h2 class="headerTitleWithLogo">codebar android tutorials</h2></a><div class="navigationWrapper navigationSlider"><nav class="slidingNav"><ul class="nav-site nav-site-internal"><li class=""><a href="/android-tutorials/docs/setup" target="_self">Getting Setup</a></li><li class="siteNavGroupActive"><a href="/android-tutorials/docs/cookie-clicker-kotlin" target="_self">Kotlin Tutorials</a></li><li class=""><a href="/android-tutorials/blog/" target="_self">Blog</a></li></ul></nav></div></header></div></div><div class="navPusher"><div class="docMainWrapper wrapper"><div class="docsNavContainer" id="docsNav"><nav class="toc"><div class="toggleNav"><section class="navWrapper wrapper"><div class="navBreadcrumb wrapper"><div class="navToggle" id="navToggler"><div class="hamburger-menu"><div class="line1"></div><div class="line2"></div><div class="line3"></div></div></div><h2><i>›</i><span>Java Tutorials (deprecated)</span></h2><div class="tocToggler" id="tocToggler"><i class="icon-toc"></i></div></div><div class="navGroups"><div class="navGroup"><h3 class="navGroupCategoryTitle">Kotlin Tutorials</h3><ul class=""><li class="navListItem"><a class="navItem" href="/android-tutorials/docs/cookie-clicker-kotlin">Cookie Clicker (Kotlin)</a></li><li class="navListItem"><a class="navItem" href="/android-tutorials/docs/krumble">Krumble</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Java Tutorials (deprecated)</h3><ul class=""><li class="navListItem"><a class="navItem" href="/android-tutorials/docs/cookie-clicker-java">Cookie Clicker (Java)</a></li><li class="navListItem"><a class="navItem" href="/android-tutorials/docs/layouts">Android Layouts</a></li><li class="navListItem"><a class="navItem" href="/android-tutorials/docs/maps">Google Maps</a></li><li class="navListItem navListItemActive"><a class="navItem" href="/android-tutorials/docs/activity-lifecycle">Activity Lifecycle</a></li><li class="navListItem"><a class="navItem" href="/android-tutorials/docs/espresso">Espresso UI Testing</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Other Resources</h3><ul class=""><li class="navListItem"><a class="navItem" href="/android-tutorials/docs/more-resources">Other Resources</a></li></ul></div></div></section></div><script>
var coll = document.getElementsByClassName('collapsible');
var checkActiveCategory = true;
for (var i = 0; i < coll.length; i++) {
var links = coll[i].nextElementSibling.getElementsByTagName('*');
if (checkActiveCategory){
for (var j = 0; j < links.length; j++) {
if (links[j].classList.contains('navListItemActive')){
coll[i].nextElementSibling.classList.toggle('hide');
coll[i].childNodes[1].classList.toggle('rotate');
checkActiveCategory = false;
break;
}
}
}
coll[i].addEventListener('click', function() {
var arrow = this.childNodes[1];
arrow.classList.toggle('rotate');
var content = this.nextElementSibling;
content.classList.toggle('hide');
});
}
document.addEventListener('DOMContentLoaded', function() {
createToggler('#navToggler', '#docsNav', 'docsSliderActive');
createToggler('#tocToggler', 'body', 'tocActive');
var headings = document.querySelector('.toc-headings');
headings && headings.addEventListener('click', function(event) {
var el = event.target;
while(el !== headings){
if (el.tagName === 'A') {
document.body.classList.remove('tocActive');
break;
} else{
el = el.parentNode;
}
}
}, false);
function createToggler(togglerSelector, targetSelector, className) {
var toggler = document.querySelector(togglerSelector);
var target = document.querySelector(targetSelector);
if (!toggler) {
return;
}
toggler.onclick = function(event) {
event.preventDefault();
target.classList.toggle(className);
};
}
});
</script></nav></div><div class="container mainContainer"><div class="wrapper"><div class="post"><header class="postHeader"><h1 class="postHeaderTitle">Activity Lifecycle</h1></header><article><div><span><p>#Android Dev - Activity Lifecycle</p>
<p>(this is a mini tutorial that can be completed at any time)</p>
<h2><a class="anchor" aria-hidden="true" id="1-intro"></a><a href="#1-intro" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>1. Intro</h2>
<p>The aim of this tutorial is to look at how the activity lifecycle works which looks like this:</p>
<p><img src="https://d2mxuefqeaa7sj.cloudfront.net/s_E25ED0FC0DF5A9B6B084CB936BAE886D8F9B2E37A2183F438D75F8F9BC39F410_1487708990139_file.png" alt=""></p>
<blockquote>
<p>for more information take a look at (<a href="https://developer.android.com/guide/components/activities/activity-lifecycle.html">https://developer.android.com/guide/components/activities/activity-lifecycle.html</a>) There's also a really great video by Kristin Marsicano (<a href="https://realm.io/news/activities-in-the-wild-exploring-the-activity-lifecycle-android/">https://realm.io/news/activities-in-the-wild-exploring-the-activity-lifecycle-android/</a>)</p>
</blockquote>
<h2><a class="anchor" aria-hidden="true" id="2-logging"></a><a href="#2-logging" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>2. Logging</h2>
<p>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 <kbd>cmd</kbd>+<kbd>n</kbd> and on Windows press <kbd>Alt</kbd>+<kbd>Insert</kbd> . Here you can select <code>Override methods</code> and find the methods listed above.</p>
<p>We’re then going to write ourselves some Log messages. If you’ve ever written any JavaScript, this is the same as <code>console.log</code>. 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.</p>
<p>When you first look in the Android Monitor, you’ll notice <strong>a lot</strong> of messages coming through. We can filter out which ones we care about by using a tag, like so:</p>
<p><img src="https://d2mxuefqeaa7sj.cloudfront.net/s_E25ED0FC0DF5A9B6B084CB936BAE886D8F9B2E37A2183F438D75F8F9BC39F410_1487710556648_Screen+Shot+2017-02-21+at+20.55.42.png" alt=""></p>
<p><img src="https://d2mxuefqeaa7sj.cloudfront.net/s_E25ED0FC0DF5A9B6B084CB936BAE886D8F9B2E37A2183F438D75F8F9BC39F410_1487709397176_test.png" alt=""> As you write a log message inside each method, speak to your coach about when you think this might get called!</p>
<p><img src="https://d2mxuefqeaa7sj.cloudfront.net/s_E25ED0FC0DF5A9B6B084CB936BAE886D8F9B2E37A2183F438D75F8F9BC39F410_1487708752616_Screen+Shot+2017-02-21+at+20.25.28.png" alt=""></p>
<blockquote>
<p>this is what my code looks like</p>
</blockquote>
<p>Run this on a real device (or an emulator) and test it out and answer with your coach the following questions:</p>
<ol>
<li>What methods get called when the app first starts up?</li>
<li>What methods get called if I go home and come back into the app?</li>
<li>What methods get called if I kill the app, and then open it again?</li>
</ol>
<h2><a class="anchor" aria-hidden="true" id="3-gotchas"></a><a href="#3-gotchas" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>3. Gotchas</h2>
<p>Which methods get called if you rotate your device?</p>
<p>Is that what you expected?</p>
<p>What other things could you do to your device that might destroy and re-create your activity? (Hint: changing the language)</p>
<p><img src="https://d2mxuefqeaa7sj.cloudfront.net/s_E25ED0FC0DF5A9B6B084CB936BAE886D8F9B2E37A2183F438D75F8F9BC39F410_1487709473122_test.png" alt=""> 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?</p>
<h2><a class="anchor" aria-hidden="true" id="4-pop-quiz"></a><a href="#4-pop-quiz" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>4. Pop quiz</h2>
<p>For the following questions, do you think they are true or false?</p>
<ol>
<li>After <strong>onCreate</strong>, the activity is now visible to the user to interact with.</li>
<li><strong>onStart</strong> always gets called every time the user comes back into the activity.</li>
<li><strong>onResume</strong> always gets called when you return to the activity, regardless of whether you killed the activity or just briefly left it.</li>
<li>an activity in <strong>onPause</strong> can still be visible on the screen.</li>
<li><strong>onDestroy</strong> is the best place to save anything a user did so they can see it when they come back.</li>
</ol>
<p>Answers:</p>
<p>ǝnɹʇ ˙ʇɐƃnou uı sddɐ pǝʍopuıʍ ıʇןnɯ 'ǝnɹʇ 4 ˙ǝnɹʇ 3 ˙ǝsןɐɟ 2 ˙ǝɯnsǝɹuo ɹǝʇɟɐ s’ʇı 'ǝsןɐɟ 1</p>
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/android-tutorials/docs/maps"><span class="arrow-prev">← </span><span>Google Maps</span></a><a class="docs-next button" href="/android-tutorials/docs/espresso"><span>Espresso UI Testing</span><span class="arrow-next"> →</span></a></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#1-intro">1. Intro</a></li><li><a href="#2-logging">2. Logging</a></li><li><a href="#3-gotchas">3. Gotchas</a></li><li><a href="#4-pop-quiz">4. Pop quiz</a></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/android-tutorials/docs/en/attributions.html">Attributions</a></section><a href="https://codebar.io/" target="_blank" rel="noreferrer noopener" class="fbOpenSource"><img src="/android-tutorials/img/codebar-full-white-logo.png" alt="codebar"/></a></footer></div></body></html>