<!DOCTYPE html>

<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head><link rel="canonical" href="http://msdn.microsoft.com/en-us/library/windows/apps/dn263169.aspx" />
<title>Manage life cycle and state in a C++ Windows Store app (tutorial 2 of 4) (Windows)</title>
<script type="text/javascript" src="http://i2.msdn.microsoft.com/Areas/Epx/Content/Scripts/modernizr-2.6.2-msdn.min.js" data-do-not-move="true"> </script>




<meta name="DCS.dcsuri" content="/en-us/library/windows/apps/dn263169(d=default,l=en-us,v=win.10).aspx" />

<meta name="NormalizedUrl" content="http://msdn.microsoft.com/en-us/library/windows/apps/dn263169(d=default,l=en-us,v=win.10).aspx" />


Manage life cycle and state in a C++ Windows Store app (tutorial 2 of 4) (Windows Recommended Release

<meta name="VotingContextUrl" content="http://msdn.microsoft.com/en-us/library/windows/apps/dn263169(d=default,l=en-us,v=win.10).aspx" />

<meta name="MN" content="4B9B1F31-8:13:06 PM" />

<meta name="Search.ShortId" content="dn263169" />

<meta name="Ms.Locale" content="en-us" />








<link rel="shortcut icon" href="/Areas/Epx/Themes/Windows/Content/Winlogo_favicon.ico" />

<link rel="stylesheet" type="text/css" href="http://i3.msdn.microsoft.com/Combined.css?resources=0:Topic,0:CodeSnippet,0:ExpandableCollapsibleArea,0:CommunityContent,0:TopicNotInScope,0:FeedViewerBasic,0:ImageSprite,1:Header,1:sprite,2:Toc,0:NavSidebar,2:LibraryMemberFilter,1:Footer,3:DetailedLinkList,3:LinkList,1:Windows;/Areas/Epx/Content/Css:0,/Areas/Epx/Themes/Windows/Content:1,/Areas/Epx/Library/Content:2,/Areas/Epx/Themes/Base/Content:3&amp;amp;hashKey=D0AB82E739E0ECD65FC20DB8E2C1C954" /></head>
<body class="library IE IE8">
<div id="page">






<div id="ux-header" class="IE IE8 ltr">
<div id="headerContainer">
<div id="logoAndSearchBox">
<div id="logo">
<a href="http://msdn.microsoft.com/windows/apps/" title="Windows"><img alt="Windows" src="http://i.msdn.microsoft.com/Areas/Epx/Themes/Windows/Content/Windows_logo.png"></img></a>
</div>

<div id="selectedTab">
<a class="dropDownToggle" href="javascript:void(0);">Dev Center - Windows Store apps</a>
</div>
<div id="toclevel1_menu" style="display: none;">
<ul>
<li><a href="http://msdn.microsoft.com/windows/" title="Dev Center Home">Dev Center Home</a></li>
<li><a href="http://msdn.microsoft.com/windows/apps/" title="Windows Store apps">Windows Store apps</a></li>
<li><a href="http://msdn.microsoft.com/ie/" title="Internet Explorer">Internet Explorer</a></li>
<li><a href="http://msdn.microsoft.com/windows/desktop/" title="Desktop">Desktop</a></li>
<li><a href="http://msdn.microsoft.com/windows/hardware/" title="Hardware">Hardware</a></li>
</ul>
<div style="clear: both"></div>
</div>






<div class="SearchBox">
<form id="HeaderSearchForm" name="HeaderSearchForm" method="get" action="http://social.msdn.microsoft.com/Search" onsubmit="return Epx.Controls.SearchBox.searchBoxOnSubmit(this, this.title);">
<input id="HeaderSearchTextBox" name="query" type="text" maxlength="200" onfocus="Epx.Controls.SearchBox.watermarkFocus(this, this.title, 'SearchBoxOnFocus')" onblur="Epx.Controls.SearchBox.watermarkBlur(this, this.title, 'SearchBoxOnFocus')" />
<input id="RefinementId" name="refinement" type="hidden" value="180" />
<button id="HeaderSearchButton" value="" type="submit" class="header-search-button"></button>
</form>


</div>

<div class="Clear"></div>
</div>
<div class="Clear"></div>
<div class="ux-internav">
<div class="toclevel2">
<a class="normal" href="https://appdev.microsoft.com/storeportals/" title="Dashboard">Dashboard</a>
<a class="active" href="http://msdn.microsoft.com/windows/apps/br211386" title="Get started ">Get started </a>
<a class="normal" href="http://msdn.microsoft.com/windows/apps/hh779072" title="Design">Design</a>
<a class="normal" href="http://msdn.microsoft.com/windows/apps/br229519" title="Develop">Develop</a>
<a class="normal" href="http://msdn.microsoft.com/windows/apps/br230836" title="Publish">Publish</a>
</div>
<div class="clear"></div>

</div>
<div id="signIn"><a class="scarabLink" href="http://login.live.com/logout.srf?ct=1387944787&amp;rver=6.0.5276.0&amp;lc=1033&amp;id=254354&amp;ru=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fwindows%2fapps%2fdn263169.aspx&amp;mkt=en-US" title="Sign out">Sign out</a><a class=" createProfileLink" href="http://social.msdn.microsoft.com/profile/briechenstein%20%20enterprise%20architecture%20repository/" title="Briechenstein Enterprise Architecture Repository">Briechenstein Enterprise Architecture Repository</a></div>
</div>
<div id="blackBar">
<div class="toclevel3">
<a class="normal" href="http://msdn.microsoft.com/windows/apps/hh974576" title="Meet Windows Store apps">Meet Windows Store apps</a>
<a class="normal" href="http://msdn.microsoft.com/library/windows/apps/jj193592" title="Sign up">Sign up</a>
<a class="active" href="http://msdn.microsoft.com/library/windows/apps/bg125376" title="Your first app">Your first app</a>
<a class="normal" href="http://msdn.microsoft.com/windows/apps/jj679957" title="The basics">The basics</a>
<a class="normal" href="http://msdn.microsoft.com/library/windows/apps/bg182410" title="Windows 8.1 Feature Guide">Windows 8.1 Feature Guide</a>
</div>
<div class="clear"></div>

</div>
</div>



<div id="body">








<div id="leftNav">



<div id="tocnav">
<div class="toclevel0" data-toclevel="0">
<a data-tochassubtree="true" href="/en-us/library/windows/apps/hh974580.aspx" id="bg126394Win.10en-us" mtpsaliasid="" mtpsassetid="1FC11A62-2B11-42F3-8BD4-2C9C7A6E235FWin.10en-us" mtpsshortid="hh974580WIN.10en-us" title="Choose C++">Choose C++</a> </div>
<div class="toclevel1" data-toclevel="1">
<a href="/en-us/library/windows/apps/dn263168.aspx" mtpsaliasid="" mtpsassetid="" mtpsshortid="" title="Part 1: Create a &quot;Hello, world&quot; app">Part 1: Create a "Hello, world" app</a> </div>
<div class="toclevel1 current" data-toclevel="1">
<a href="/en-us/library/windows/apps/dn263169.aspx" mtpsaliasid="" mtpsassetid="" mtpsshortid="" title="Part 2: Manage app lifecycle and state">Part 2: Manage app lifecycle and state</a> </div>
<div class="toclevel1" data-toclevel="1">
<a href="/en-us/library/windows/apps/dn263172.aspx" mtpsaliasid="" mtpsassetid="" mtpsshortid="" title="Part 3: Navigation, layout, and views">Part 3: Navigation, layout, and views</a> </div>
<div class="toclevel1" data-toclevel="1">
<a href="/en-us/library/windows/apps/dn263165.aspx" mtpsaliasid="" mtpsassetid="" mtpsshortid="" title="Part 4: File access and pickers">Part 4: File access and pickers</a> </div>
<div class="toclevel1" data-toclevel="1">
<a href="/en-us/library/windows/apps/hh465045.aspx" mtpsaliasid="" mtpsassetid="" mtpsshortid="" title="Part 5: Create a blog reader">Part 5: Create a blog reader</a> </div>
<div class="toclevel1" data-toclevel="1">
<a href="/en-us/library/windows/apps/hh700360.aspx" mtpsaliasid="" mtpsassetid="" mtpsshortid="" title="Doc roadmap">Doc roadmap</a> </div>
</div>






<div id="toc-resizable-ew" class="toc-resizable-ew"></div>

</div>
<div id="content" class="content">









<div class="topic" xmlns="http://www.w3.org/1999/xhtml">
<h1 class="title">Manage life cycle and state in a C++ Windows Store app (tutorial 2 of 4)</h1>

<div id="mainSection">
<div class="clsServerSDKContent">

</div>
<div class="NavSidebar" xmlns=""><span class="sidebarheading" xmlns="http://www.w3.org/1999/xhtml">In this article</span>
<div class="sidebar_toc" xmlns="http://www.w3.org/1999/xhtml">
<p>
</p><ul>
<li><a href="#beforeyoustart...">Before you start...</a></li>
<li><a href="#abouttheappslife_cycle">About the app's life cycle</a></li>
<li><a href="#step1usingsuspensionmanager">Step 1: Using SuspensionManager</a></li>
<li><a href="#step2savetheapps_state">Step 2: Save the app's state</a></li>
<li><a href="#step3restoretheapps_state">Step 3: Restore the app's state</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#seethecode">See the code</a></li>
<li><a href="#next_steps">Next steps</a></li>
</ul>

</div>
<span class="sidebar_heading" xmlns="http://www.w3.org/1999/xhtml">Important APIs</span>
<div class="sidebar_summary" xmlns="http://www.w3.org/1999/xhtml">
<p>
</p><ul>
<li>
<a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localsettings.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">LocalSettings</strong></a>
</li>
<li>
<a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.roamingsettings.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">RoamingSettings</strong></a>
</li>
</ul>

</div>
</div>
<p>
</p><p>In this part of the C++ tutorial series, you update your "Hello, world" app to respond to life cycle events, and save user and session data. </p>

<p>In Windows 8.1, you can launch several apps and switch between them without worrying about slowing down the system or running down the battery. That's because the system automatically suspends (and sometimes closes) apps that are running in the background. A well-designed app can be suspended, closed, and restarted by the system and still seem as though it was running the entire time. </p>
<p></p>
<h2><a id="Beforeyoustart..."></a><a id="beforeyoustart..."></a><a id="BEFOREYOUSTART..."></a>Before you start...</h2>
<p>
</p><ul>
<li>This is the second tutorial in a series. Before you start this tutorial, read <a href="http://msdn.microsoft.com/en-us/library/windows/apps/dn263168.aspx">Hello World in C++</a>.</li>
<li>You can see the complete code for this tutorial in the <a href="http://go.microsoft.com/fwlink/p/?LinkId=327685">Hello World (C++)</a> sample on Code Gallery.</li>
</ul>

<h2><a id="Abouttheappslifecycle"></a><a id="abouttheappslifecycle"></a><a id="ABOUTTHEAPPSLIFE_CYCLE"></a>About the app's life cycle</h2>
<p>Before you go back to the code, let's talk a bit about the app's life cycle. At any given point, an app is either not running, running, or suspended. Activation is the term that's used for the transition from not running to running; activation marks the beginning of the app's life cycle. </p>
<p><img id="jstutstate_diagram" alt="App life cycle" src="http://i.msdn.microsoft.com/dynimg/IC619570.png" title="App life cycle" xmlns="" /></p>
<p>When an app is running, it can be suspended when the user switches away from it or when Windows enters a low power state. While your app is suspended, it continues to reside in memory so that the user can quickly and reliably switch between suspended apps, and resume them. When your app is suspended and then resumed, you don't have to write any extra code to make it look as though it had been running the entire time.</p>
<p>But Windows can also close a suspended app at any time to free up memory for other apps or to save power. When your app is closed, it's unloaded from memory. When the user starts it again, the system must activate it, and that might mean that the app data and user data must be reloaded.</p>
<p>When the user closes an app by pressing Alt+F4 or by using the close gesture, the app is suspended for 10 seconds and then is closed. </p>
<p>Windows notifies your app when it suspends it, but doesn't provide additional notification when it closes the app. That means your app must handle the suspended event and use it to save its state and release its exclusive resources and file handles immediately. </p>
<p>To create a good user experience, you want your app to look like it never stopped running. The app should retain any data the user entered, settings they changed, and so on. That means you must save your app's state when it's suspended, in case Windows closes it, so that you can restore its state later. </p>
<p>There are two types of data for you to manage in your app: <em>app data</em> and <em>session data</em>. </p>
<p>The next steps show how to update the app to save these types of data. What state do you have to save? Right now, the only thing the user can change is his or her name entry. The user can also choose the <strong>Say "Hello"</strong> button to generate a personalized greeting.</p>
<h2><a id="Step1UsingSuspensionManager"></a><a id="step1usingsuspensionmanager"></a><a id="STEP1USINGSUSPENSIONMANAGER"></a>Step 1: Using SuspensionManager</h2>
<p>In <a href="http://msdn.microsoft.com/en-us/library/windows/apps/dn263168.aspx">Hello World in C++</a>, you replaced the default MainPage file with one based on the <strong>Basic Page</strong> template. When you used the <strong>Basic Page</strong> template, Visual Studio added several files to the project in the \Common\ folder. One of these files contains the <code>SuspensionManager</code> class, which is a helper class that simplifies life-cycle management for the app. It does several things for you. It saves and restores the navigation state of the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a> that hosts the app pages. In a one-page app, saving the navigation state doesn't seem very important, but it becomes very important when you add multiple pages to an app. It also gives each page the opportunity to save and restore its state. <code>SuspensionManager</code> serializes the page state data and writes it to an XML file in your app's local storage.</p>
<p>To use the <code>SuspensionManager</code> class in the app, you first have to register the main app <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a>. When you're done, <code>SuspensionManager</code> knows about each page in the app, and it can save and restore the navigation state. You register the <strong>Frame</strong> immediately after it's created in the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.onlaunched.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">OnLaunched</strong></a> method in App.xaml.cpp.</p>
<p class="proch"><img id="wedge" alt="Dn263169.wedge(en-us,WIN.10).gif" src="http://i.msdn.microsoft.com/areas/global/content/clear.gif" title="Dn263169.wedge(en-us,WIN.10).gif" xmlns="" class="cl_IC153696" /><strong>To use SuspensionManager</strong></p>
<ol>
<li>In <strong>Solution Explorer</strong>, open App.xaml.cpp.</li>
<li>Add this #include directive:
<div id="code-snippet-1" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_f0a31bd0-e344-46c9-aa7d-eb390a82ef0b');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_f0a31bd0-e344-46c9-aa7d-eb390a82ef0b" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

#include <span style="color:#A31515;">"Common\SuspensionManager.h"</span>

</pre></div>

</div>
</div>
</div>

</li>
<li>In the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.onlaunched.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">OnLaunched</strong></a> method, call the <code>SuspensionManager::RegisterFrame</code> static method to register the root <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a>. Place this statement directly after the <code>rootFrame = ref new Frame();</code> statement.
<div id="code-snippet-2" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_112fd954-3fc2-4498-b58c-96df4437689f');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_112fd954-3fc2-4498-b58c-96df4437689f" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

HelloWorld::Common::SuspensionManager::RegisterFrame(rootFrame, <span style="color:#A31515;">"appFrame"</span>);

</pre></div>

</div>
</div>
</div>

</li>
</ol>
<h2><a id="Step2Savetheappsstate"></a><a id="step2savetheappsstate"></a><a id="STEP2SAVETHEAPPS_STATE"></a>Step 2: Save the app's state</h2>
<p>Generally, there are two types of data for you to manage in your app: <em>app data</em> and <em>session data</em>. App data is persistent across sessions and must always be accessible to the user. In your app, the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textbox.text.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Text</strong></a> of the <code>nameInput</code> <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textbox.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">TextBox</strong></a> is app data. Always save important app data incrementally throughout the life of your app. Because your app has only up to five seconds to run code in the suspending event handler, you have to ensure that your important app data is saved to persistent storage before the app is suspended. </p>
<p>Windows provides a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Windows.Storage.ApplicationData</strong></a> object to help you manage your app data. This object has a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.roamingsettings.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">RoamingSettings</strong></a> property that returns an <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdatacontainer.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">ApplicationDataContainer</strong></a>. You can use this <strong>ApplicationDataContainer</strong> to store app data that persists across sessions. Let's store the user's name in the roaming <strong>ApplicationDataContainer</strong> as the user types it in. </p>
<p class="note"><strong>Note</strong> This tutorial shows how to use <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.roamingsettings.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">RoamingSettings</strong></a>. The roaming settings app data container makes it easy to store data in a way that's accessible to the user across multiple machines. Basically, the data is uploaded to the cloud in the background. You can also use the local settings app data container (<a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localsettings.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">LocalSettings</strong></a>), but only when you want to store machine-specific info.</p>
<p>Save persistent app data as often as it makes sense in your app. Here, you handle the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textbox.textchanged.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">TextBox::TextChanged</strong></a> event and save the user name as the user enters it.</p>
<p class="proch"><img id="wedge" alt="Dn263169.wedge(en-us,WIN.10).gif" src="http://i.msdn.microsoft.com/areas/global/content/clear.gif" title="Dn263169.wedge(en-us,WIN.10).gif" xmlns="" class="cl_IC153696" /><strong>To save app data</strong></p>
<ol>
<li>In <strong>Solution Explorer</strong>, open MainPage.xaml.</li>
<li>In either XAML or design view, select the <code>nameInput</code> <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textbox.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">TextBox</strong></a> that you added to MainPage.xaml.</li>
<li>In the <strong>Properties</strong> window, choose the <strong>Events</strong> button (<img id="EventsButton" alt="Events button" src="http://i.msdn.microsoft.com/dynimg/IC580063.png" title="Events button" xmlns="" />).</li>
<li>Find the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textbox.textchanged.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">TextChanged</strong></a> event in the event list. In the text box for the event, type "NameInput_TextChanged" as the name of the function that handles the <strong>TextChanged</strong> event.</li>
<li>Press Enter. The event handler method is created in MainPage.xaml.cpp and opened in the code editor so that you can add the following code, which is executed when the event occurs. In the event handler, you save the <code>nameInput</code> text in <code>roamingSettings</code>.
<div id="code-snippet-3" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_a7c00f7d-d485-47e2-b486-c5522359e297');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_a7c00f7d-d485-47e2-b486-c5522359e297" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>


<span style="color:Blue;">auto</span> roamingSettings =
Windows::Storage::ApplicationData::Current-&gt;RoamingSettings;
roamingSettings-&gt;Values-&gt;Insert(<span style="color:#A31515;">"userName"</span>, nameInput-&gt;Text);
<span style="color:Blue;">if</span> (roamingSettings-&gt;Values-&gt;HasKey(<span style="color:#A31515;">"userName"</span>))
{
nameInput-&gt;Text = roamingSettings-&gt;Values-&gt;Lookup(<span style="color:#A31515;">"userName"</span>)-&gt;ToString();
}

</pre></div>

</div>
</div>
</div>

</li>
<li>Press F5 to build and run the app. Your name is saved as you enter it in the text box. </li>
</ol>
<p>Session data is temporary data that is relevant to the user’s current session in your app. A session ends when the user closes the app, reboots the computer, or logs off the computer. In your app, the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textblock.text.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Text</strong></a> of the <code>greetingOutput</code> <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textblock.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">TextBlock</strong></a> is session data. You restore it only if Windows suspends and closes the app. You have to save the navigation state of the app <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a>, so that the app can be restored to the same page is was on, and so that the <code>SuspensionManager</code> knows which page to restore the state of. You also have to save the state of the page itself. This is where you save the <code>greetingOutput</code> text. You use the <code>SuspensionManager</code> class to save session state in the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.suspending.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Application.Suspending</strong></a> event handler.</p>
<p>(For more info about the other ways you can save state, see <a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh465109.aspx">Managing app data</a> and <a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh781225.aspx">Working with state efficiently</a>.)</p>
<p>The App.xaml.cpp file contains a handler for the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.suspending.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Application.Suspending</strong></a> event. This event handler gets called when Windows is about to suspend your app. This is your opportunity to save the state of the app in case it gets closed. You use the <code>SuspensionManager</code> class to simplify saving session state. It saves the navigation state of the app, and gives you the opportunity to save the session state of the active page.</p>
<p class="proch"><img id="wedge" alt="Dn263169.wedge(en-us,WIN.10).gif" src="http://i.msdn.microsoft.com/areas/global/content/clear.gif" title="Dn263169.wedge(en-us,WIN.10).gif" xmlns="" class="cl_IC153696" /><strong>To save the session state</strong></p>
<ol>
<li>In <strong>Solution Explorer</strong>, open App.xaml.cpp.</li>
<li>Add these directives at the top of the file:
<div id="code-snippet-4" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_0bd34e53-96df-4bb1-92fd-40602a1e6201');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_0bd34e53-96df-4bb1-92fd-40602a1e6201" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

#include &lt;ppltasks.h&gt;
<span style="color:Blue;">using</span> <span style="color:Blue;">namespace</span> concurrency;

</pre></div>

</div>
</div>
</div>

</li>
<li>In the <code>OnSuspending</code> event handler method, add the following lines:
<div id="code-snippet-5" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_b911f273-d9b8-4b5d-b9fc-ad4ec933491d');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_b911f273-d9b8-4b5d-b9fc-ad4ec933491d" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>


<span style="color:Green;">//TODO: Save application state and stop any background activity</span>
<span style="color:Blue;">auto</span> deferral = e -&gt; SuspendingOperation-&gt;GetDeferral();
create_task(HelloWorld::Common::SuspensionManager::SaveAsync())
.then(deferral() {deferral-&gt;Complete(); });


</pre></div>

</div>
</div>
</div>

<p>Calling <code>SaveAsync</code> saves the navigation state of the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a> and then gives your <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.page.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Page</strong></a> the opportunity to save its content in its SaveState method.</p>
</li>
<li>To save the page state, in <strong>Solution Explorer</strong>, open MainPage.xaml.cpp and then add this code to the <code>SaveState</code> method:
<div id="code-snippet-6" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_bcc6d89c-da36-423f-9e41-a3346d837343');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_bcc6d89c-da36-423f-9e41-a3346d837343" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>


<span style="color:Green;">// Insert adds a new key or replaces value if key already exists.</span>
e-&gt;PageState-&gt;Insert(<span style="color:#A31515;">"greetingOutputText"</span>, greetingOutput-&gt;Text);


</pre></div>

</div>
</div>
</div>

<p>The <code>SuspensionManager</code> class serializes and saves the <code>pageState</code> dictionary to an XML file. Data saved in <code>pageState</code> is saved only for this session. You save the <code>greetingOutput</code> text here.</p>
</li>
<li>Choose <strong>Build</strong> &gt; <strong>Build solution</strong> to make sure the app builds with no errors.</li>
</ol>
<p>That's all you have to do to save your app's state before your app is closed. Now you can learn how to restore your app's state the next time the user starts it. </p>
<h2><a id="Step3Restoretheappsstate"></a><a id="step3restoretheappsstate"></a><a id="STEP3RESTORETHEAPPS_STATE"></a>Step 3: Restore the app's state</h2>
<p>Earlier, you saw that the App.xaml.cpp file contains code that handles your app's activation. There are many different ways to activate an app. Here we look at the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.activation.activationkind.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Launch</strong></a> activation and the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.onlaunched.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">OnLaunched</strong></a> method.</p>
<p>An app is launched whenever it wasn't running and the user activates it. When an app is launched, Windows displays its splash screen.</p>
<p>In App.xaml.cpp, let's look at the code that handles app activation. The code defines an override of the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.onlaunched.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">OnLaunched</strong></a> method. The code in this method is executed only if the activation is a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.activation.activationkind.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Launch</strong></a> activation. (You can override other methods to handle other kinds of activation, but we don't do it here. For more info, see <a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx">Application lifecycle</a>.)</p>
<p>First, the code declares a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a> and tries to assign the current window contents to it.</p>

<div id="code-snippet-7" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_b3058310-6f92-497c-8d58-870fbf50611e');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_b3058310-6f92-497c-8d58-870fbf50611e" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

<span style="color:Blue;">auto</span> rootFrame = <span style="color:Blue;">dynamic_cast</span>&lt;Frame^&gt;(Window::Current-&gt;Content);

</pre></div>

</div>
</div>
</div>

<p>If the window already contains a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a>, the app is already initialized and so <strong>Frame</strong> initialization is skipped.</p>
<p>If the app isn't initialized, the code creates a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a> to host the app pages. You register the <strong>Frame</strong> with the <code>SuspensionManager</code>. This is the code you added in Step 1.</p>
<p>Next, the code checks the previous execution state to see how the app was last closed. When the previous execution state is <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.activation.applicationexecutionstate.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Terminated</strong></a>, it means that the last time the app ran, Windows successfully suspended it and then closed it. In that case, you have to restore the app's state. </p>

<div id="code-snippet-8" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_16bf626b-97dc-4733-956c-8e485d657bb1');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_16bf626b-97dc-4733-956c-8e485d657bb1" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

<span style="color:Green;">// Do not repeat app initialization when the Window already has content,</span>
<span style="color:Green;">// just ensure that the window is active</span>
<span style="color:Blue;">if</span> (rootFrame == <span style="color:Blue;">nullptr</span>)
{
<span style="color:Green;">// Create a Frame to act as the navigation context and associate it with</span>
<span style="color:Green;">// a SuspensionManager key</span>
rootFrame = ref <span style="color:Blue;">new</span> Frame();
HelloWorld::Common::SuspensionManager::RegisterFrame(rootFrame, <span style="color:#A31515;">"appFrame"</span>);

<span style="color:Blue;">if</span> (e-&gt;PreviousExecutionState == ApplicationExecutionState::Terminated)
{
<span style="color:Green;">// TODO: Restore the saved session state only when appropriate, scheduling the</span>
<span style="color:Green;">// final launch steps after the restore is complete</span>

}

<span style="color:Blue;">if</span> (rootFrame-&gt;Content == <span style="color:Blue;">nullptr</span>)
{
<span style="color:Green;">// When the navigation stack isn't restored navigate to the first page,</span>
<span style="color:Green;">// configuring the new page by passing required information as a navigation</span>
<span style="color:Green;">// parameter</span>
<span style="color:Blue;">if</span> (!rootFrame-&gt;Navigate(TypeName(MainPage::<span style="color:Blue;">typeid</span>), e-&gt;Arguments))
{
<span style="color:Blue;">throw</span> ref <span style="color:Blue;">new</span> FailureException(<span style="color:#A31515;">"Failed to create initial page"</span>);
}
}

<span style="color:Green;">// Place the frame in the current Window</span>
Window::Current-&gt;Content = rootFrame;

<span style="color:Green;">// Ensure the current window is active</span>
Window::Current-&gt;Activate();
}


</pre></div>

</div>
</div>
</div>

<p>Next, the code checks whether the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a> contains any content. If the app is running, or the navigation state was restored, the <strong>Frame</strong> already has content. Otherwise, the <strong>Frame</strong> navigates to the first page in the app. In this case, it navigates to MainPage.</p>

<div id="code-snippet-9" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_f75ddf72-0753-4305-9a11-0acd4d87c9fd');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_f75ddf72-0753-4305-9a11-0acd4d87c9fd" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

<span style="color:Blue;">if</span> (rootFrame-&gt;Content == <span style="color:Blue;">nullptr</span>)
{
<span style="color:Green;">// When the navigation stack isn't restored navigate to the first page,</span>
<span style="color:Green;">// configuring the new page by passing required information as a navigation</span>
<span style="color:Green;">// parameter</span>
<span style="color:Blue;">if</span> (!rootFrame-&gt;Navigate(TypeName(MainPage::<span style="color:Blue;">typeid</span>), e-&gt;Arguments))
{
<span style="color:Blue;">throw</span> ref <span style="color:Blue;">new</span> FailureException(<span style="color:#A31515;">"Failed to create initial page"</span>);
}
}


</pre></div>

</div>
</div>
</div>

<p>Finally, the code activates the window.</p>

<div id="code-snippet-10" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_7f1bd552-3f27-47dd-afce-f5f5a5bc85d2');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_7f1bd552-3f27-47dd-afce-f5f5a5bc85d2" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>


<span style="color:Green;">// Ensure the current window is active</span>
Window::Current-&gt;Activate();


</pre></div>

</div>
</div>
</div>

<p>Now that you know what happens when the app is started, let's look at how to restore the app state.</p>
<p class="proch"><img id="wedge" alt="Dn263169.wedge(en-us,WIN.10).gif" src="http://i.msdn.microsoft.com/areas/global/content/clear.gif" title="Dn263169.wedge(en-us,WIN.10).gif" xmlns="" class="cl_IC153696" /><strong>To restore the app's state</strong></p>
<ol>
<li>If the app was closed, call the <code>SuspensionManager::RestoreAsync</code> method.<p>Calling <code>RestoreAsync</code> restores the navigation state of the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Frame</strong></a> and then gives the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.page.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Page</strong></a> the opportunity to restore its content.</p>

<div id="code-snippet-11" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_d3a2ba1e-1ea1-4c7f-b911-3694dc5f6e38');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_d3a2ba1e-1ea1-4c7f-b911-3694dc5f6e38" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

<span style="color:Blue;">if</span> (e-&gt;PreviousExecutionState == ApplicationExecutionState::Terminated)
{
<span style="color:Green;">// TODO: Restore the saved session state only when appropriate, scheduling the</span>
<span style="color:Green;">// final launch steps after the restore is complete</span>
HelloWorld::Common::SuspensionManager::RestoreAsync();


</pre></div>

</div>
</div>
</div>

</li>
<li>In MainPage.xaml.cpp, add code to the <code>LoadState</code> method to restore the page state.<ol>
<li>First, check to see whether the <code>pageState</code> dictionary exists and has a key named <code>greetingOutputText</code>. If the key exists, use it to restore the <code>greetingOutput</code> text.
<div id="code-snippet-12" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_61dc048c-4c2b-418c-9f1e-2aa8bdb68e95');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_61dc048c-4c2b-418c-9f1e-2aa8bdb68e95" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>


<span style="color:Green;">// Restore values stored in session state.</span>
<span style="color:Blue;">if</span>( e-&gt;PageState != <span style="color:Blue;">nullptr</span> &amp;&amp; e-&gt;PageState-&gt;HasKey(<span style="color:#A31515;">"greetingOutputText"</span>))
{
greetingOutput-&gt;Text = e-&gt;PageState-&gt;Lookup(<span style="color:#A31515;">"greetingOutputText"</span>)-&gt;ToString();
}


</pre></div>

</div>
</div>
</div>

</li>
<li>Next, load the user name. Because you want the user name data to persist over multiple sessions, you store it in the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.roamingsettings.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">RoamingSettings</strong></a> app data container. Let's add some code to see whether the user name exists and, if it does, to display it.
<div id="code-snippet-13" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_130e7b29-038f-4a05-86b5-6ce3e5f47ab5');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_130e7b29-038f-4a05-86b5-6ce3e5f47ab5" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

<span style="color:Green;">// Restore values stored in app data.</span>
<span style="color:Blue;">auto</span> roamingSettings =
Windows::Storage::ApplicationData::Current-&gt;RoamingSettings;
<span style="color:Blue;">if</span> (roamingSettings-&gt;Values-&gt;HasKey(<span style="color:#A31515;">"userName"</span>))
{
nameInput-&gt;Text = roamingSettings-&gt;Values-&gt;Lookup(<span style="color:#A31515;">"userName"</span>)-&gt;ToString();
}


</pre></div>

</div>
</div>
</div>

</li>
</ol>
<p>Here's the complete code for the <code>LoadState</code> method.</p>

<div id="code-snippet-14" class="codeSnippetContainer" xmlns="">
<div class="codeSnippetContainerTabs">
<div class="codeSnippetContainerTabSingle" dir="ltr"><a>C++</a></div>
</div>
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="Copy to clipboard." href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_40508695-0c5c-45f3-9a54-b4c65ce73a5a');">Copy</a>
</div>
</div>
<div id="CodeSnippetContainerCode_40508695-0c5c-45f3-9a54-b4c65ce73a5a" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>

<span style="color:Green;">/// &lt;summary&gt;</span>
<span style="color:Green;">/// Populates the page with content passed during navigation. Any saved state is also</span>
<span style="color:Green;">/// provided when recreating a page from a prior session.</span>
<span style="color:Green;">/// &lt;/summary&gt;</span>
<span style="color:Green;">/// &lt;param name="navigationParameter"&gt;The parameter value passed to</span>
<span style="color:Green;">/// &lt;see cref="Frame::Navigate(Type, Object)"/&gt; when this page was initially requested.</span>
<span style="color:Green;">/// &lt;/param&gt;</span>
<span style="color:Green;">/// &lt;param name="pageState"&gt;A map of state preserved by this page during an earlier</span>
<span style="color:Green;">/// session. This will be null the first time a page is visited.&lt;/param&gt;</span>
<span style="color:Blue;">void</span> MainPage::LoadState(Object^ sender, Common::LoadStateEventArgs^ e)
{
(<span style="color:Blue;">void</span>) sender; <span style="color:Green;">// Unused parameter</span>
<span style="color:Green;">// Restore values stored in session state.</span>
<span style="color:Blue;">if</span>( e-&gt;PageState != <span style="color:Blue;">nullptr</span> &amp;&amp; e-&gt;PageState-&gt;HasKey(<span style="color:#A31515;">"greetingOutputText"</span>))
{
greetingOutput-&gt;Text = e-&gt;PageState-&gt;Lookup(<span style="color:#A31515;">"greetingOutputText"</span>)-&gt;ToString();
}

<span style="color:Green;">// Restore values stored in app data.</span>
<span style="color:Blue;">auto</span> roamingSettings =
Windows::Storage::ApplicationData::Current-&gt;RoamingSettings;
<span style="color:Blue;">if</span> (roamingSettings-&gt;Values-&gt;HasKey(<span style="color:#A31515;">"userName"</span>))
{
nameInput-&gt;Text = roamingSettings-&gt;Values-&gt;Lookup(<span style="color:#A31515;">"userName"</span>)-&gt;ToString();
}
}

</pre></div>

</div>
</div>
</div>

</li>
</ol>
<p>Now you can build and run the app, and see how the session state is saved and restored. So far, you've tested your app in Visual Studio by running it in debug mode, and stopped it by selecting <strong>Debug</strong> &gt; <strong>Stop Debugging</strong>. But doing this causes the app to perform a normal shutdown, and the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.suspending.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Suspending</strong></a> event doesn't occur. Fortunately, you can use other functionality in Microsoft Visual Studio to simulate suspending, closing, and restoring an app.</p>
<p class="proch"><img id="wedge" alt="Dn263169.wedge(en-us,WIN.10).gif" src="http://i.msdn.microsoft.com/areas/global/content/clear.gif" title="Dn263169.wedge(en-us,WIN.10).gif" xmlns="" class="cl_IC153696" /><strong>To simulate suspending, closing, and restoring an app</strong></p>
<ol>
<li>Press F5 to run the app in debug mode.</li>
<li>Enter your name in the input box and choose "Say "Hello"". The greeting is displayed.</li>
<li>In Visual Studio, open the drop-down menu next to the <strong>Suspend</strong> button on the <strong>Debug Location</strong> toolbar.<p>The <strong>Debug Location</strong> toolbar appears by default while the debugger is running. If you don't see it, choose <strong>View</strong> &gt; <strong>Toolbars</strong> &gt; <strong>Debug Location</strong> to show it.</p>
<p><img id="SuspendAndShutdown" alt="Suspend and shutdown button" src="http://i.msdn.microsoft.com/dynimg/IC581844.png" title="Suspend and shutdown button" xmlns="" /></p>
</li>
<li>Select <strong>Suspend and shutdown</strong>.<p>Visual Studio simulates suspending and closing your app, so the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.suspending.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Suspending</strong></a> event occurs and your state management code is executed.</p>
</li>
<li>Press F5 to run the app again. The app is restored to its previous state.</li>
<li>Type a different name in the text box, and choose "Say "Hello"". </li>
<li>In Visual Studio, close the app by selecting <strong>Debug</strong> &gt; <strong>Stop Debugging</strong>.</li>
<li>Press F5 to run the app again.<p>Notice that the name that you last typed is restored because it was saved as you typed it in. The greeting is not restored because the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.suspending.aspx"><strong xmlns="http://www.w3.org/1999/xhtml">Suspending</strong></a> event didn't occur, and so the session state wasn't saved.</p>
</li>
</ol>
<h2><a id="Summary"></a><a id="summary"></a><a id="SUMMARY"></a>Summary</h2>
<p>Congratulations, you're done with the second tutorial! It taught how to save app data and session state in a Windows Store app.</p>
<h2><a id="Seethecode"></a><a id="seethecode"></a><a id="SEETHECODE"></a>See the code</h2>
<p>Did you get stuck, or do you want to check your work? If so, see the <a href="http://go.microsoft.com/fwlink/p/?LinkId=327685">Hello World (C++)</a> sample on Code Gallery.</p>
<h2><a id="Nextsteps"></a><a id="nextsteps"></a><a id="NEXT_STEPS"></a>Next steps</h2>
<p>The next part of this tutorial series teaches how to use the design tools in Visual Studio to create a more complex UI. See <a href="http://msdn.microsoft.com/en-us/library/windows/apps/dn263172.aspx">Navigation, layout, and views (C++)</a>.. </p>
<p> </p>
<p> </p>
<p>Build date: 12/17/2013</p>
</div>
</div>






<div class="libraryMemberFilter">
<div class="filterContainer">
<span>Show:</span>
<label>
<input type="checkbox" class="libraryFilterInherited" checked="checked" value="Inherit" />Inherited
</label>
<label>
<input type="checkbox" class="libraryFilterProtected" checked="checked" value="Protected" />Protected
</label>
</div>
</div>

<input type="hidden" id="libraryMemberFilterEmptyWarning" value="There are no members available with your current filter settings." />



</div>



</div>
<div class="clear"></div>



<input name="RequestVerificationToken" type="hidden" value="i9GEQnKRYHg34ua8ZF3SpweqHt4wDh2LlsHH5USOwCq69C2L6Ni01f4cMpeWTS3f7H12XCoj1xHVKEYzsP7G5Uep8jyPA4PXm5Fy6ab2vwEWK5rsae0tlf8yioxUfJEBf7lbARMcCwNPjTOaYrodn5Fvk1" />
<input id="ratingSubmitUrl" type="hidden" value="http://msdn.microsoft.com/en-us/library/feedback/add/dn263169.aspx" />
<input id="isTopicRated" type="hidden" value="false" />







<div id="ux-footer" class="IE IE8 ltr">

<div id="footerSock">
<div id="footerSockInner">
<a name="feedback"></a>
<div data-fragmentName="Survey" id="Fragment_Survey" xmlns="http://www.w3.org/1999/xhtml">
<div class="DetailedLink">

<div class="Content">
<div class="title">Help us help you</div>
<div class="RichText">Answer questions that we’ll use to make the Dev Center better.</div>
<div class="link">
<a href="http://go.microsoft.com/fwlink/?LinkId=316293" target="blank" id="Survey35354_1" xmlns="http://www.w3.org/1999/xhtml">Take the survey</a>
</div>
</div>
</div>
</div>
<div class="rating">
<div id="ratingSection1">
<div class="title">
Was this page helpful?
</div>
<div class="description">
Your feedback about this content is important.<br />Let us know what you think.
</div>
<div class="buttons">
<button id="ratingYes">Yes</button>
<button id="ratingNo">No</button>
</div>
<input id="ratingValue" type="hidden" value="" />
</div>
<div id="ratingSection2">
<div class="title">
Additional feedback?
</div>
<textarea id="ratingText" rows="6" cols=""></textarea>
<div class="buttons">
<button id="ratingSubmit">Submit</button>
<button id="ratingSkipThis">Skip this</button>
</div>
</div>
<div id="ratingSection3">
<div class="title">
Thank you!
</div>
<div class="description">
We appreciate your feedback.
</div>
</div>


<div id="contentFeedbackQAContainer" style="display: none;"></div>
</div>


<div data-fragmentName="SockLinks" id="Fragment_SockLinks" xmlns="http://www.w3.org/1999/xhtml">

<div class="LinkList">
<div class="Links">
<ul class="LinkColumn" style="width: 100%">
<li>
<a href="http://go.microsoft.com/fwlink/p/?LinkId=317807" id="SockLinks72842" xmlns="http://www.w3.org/1999/xhtml">Find us on Facebook</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?LinkId=317808" id="SockLinks72843" xmlns="http://www.w3.org/1999/xhtml">Follow us on Twitter</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=301425" id="SockLinks72856" xmlns="http://www.w3.org/1999/xhtml">Get the newsletter</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clear"></div>
</div>
</div>
<div id="footerContainer">
<div id="fourColumnFooter">
<div data-fragmentName="Column1" id="Fragment_Column1" xmlns="http://www.w3.org/1999/xhtml">
<div class="ListOfLinkLists">

<div class="LinkList" xmlns="http://www.w3.org/1999/xhtml">
<div class="LinkListTitle">Centers</div>
<div class="LinkListDescription"></div>
<div class="Links">
<ul class="LinkColumn" style="width: 100%">
<li>
<a href="http://msdn.microsoft.com/windows" id="Column1353549" xmlns="http://www.w3.org/1999/xhtml">Dev Center Home</a>
</li>
<li>
<a href="http://msdn.microsoft.com/windows/apps" id="Column13535410" xmlns="http://www.w3.org/1999/xhtml">Windows Store apps</a>
</li>
<li>
<a href="http://msdn.microsoft.com/ie" id="Column13535411" xmlns="http://www.w3.org/1999/xhtml">Internet Explorer</a>
</li>
<li>
<a href="http://msdn.microsoft.com/windows/desktop" id="Column13535412" xmlns="http://www.w3.org/1999/xhtml">Desktop</a>
</li>
<li>
<a href="http://msdn.microsoft.com/windows/hardware" id="Column13535413" xmlns="http://www.w3.org/1999/xhtml">Hardware</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div data-fragmentName="Column2" id="Fragment_Column2" xmlns="http://www.w3.org/1999/xhtml">
<div class="ListOfLinkLists">

<div class="LinkList" xmlns="http://www.w3.org/1999/xhtml">
<div class="LinkListTitle">Related developer sites</div>
<div class="LinkListDescription"></div>
<div class="Links">
<ul class="LinkColumn" style="width: 100%">
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=282086" id="Column23535414" xmlns="http://www.w3.org/1999/xhtml">SkyDrive</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=282089" id="Column23535415" xmlns="http://www.w3.org/1999/xhtml">Visual Studio</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=282085" id="Column23535416" xmlns="http://www.w3.org/1999/xhtml">Windows Azure</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280293" id="Column23535417" xmlns="http://www.w3.org/1999/xhtml">Windows Phone</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=282087" id="Column23535418" xmlns="http://www.w3.org/1999/xhtml">Windows Server</a>
</li>
</ul>
</div>
</div>

<div class="LinkList" xmlns="http://www.w3.org/1999/xhtml">
<div class="LinkListTitle">Other Windows sites</div>
<div class="LinkListDescription"></div>
<div class="Links">
<ul class="LinkColumn" style="width: 100%">
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280294" id="Column23535419" xmlns="http://www.w3.org/1999/xhtml">Enterprise</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280295" id="Column23535420" xmlns="http://www.w3.org/1999/xhtml">Small business</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280296" id="Column23535421" xmlns="http://www.w3.org/1999/xhtml">Students</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280297" id="Column23535422" xmlns="http://www.w3.org/1999/xhtml">Home users</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div data-fragmentName="Column3" id="Fragment_Column3" xmlns="http://www.w3.org/1999/xhtml">
<div class="ListOfLinkLists">

<div class="LinkList" xmlns="http://www.w3.org/1999/xhtml">
<div class="LinkListTitle">Downloads</div>
<div class="LinkListDescription"></div>
<div class="Links">
<ul class="LinkColumn" style="width: 100%">
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=267554" id="Column33535423" xmlns="http://www.w3.org/1999/xhtml">Windows 8.1</a>
</li>
<li>
<a href="http://www.microsoft.com/click/services/Redirect2.ashx?CREAC=300135396" id="Column335354_24" xmlns="http://www.w3.org/1999/xhtml">Visual Studio Express 2013</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=328183" id="Column33535425" xmlns="http://www.w3.org/1999/xhtml">Windows SDK</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=302156" id="Column372857" xmlns="http://www.w3.org/1999/xhtml">Code samples</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=285610" id="Column33535426" xmlns="http://www.w3.org/1999/xhtml">Design downloads</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280676" id="Column33535427" xmlns="http://www.w3.org/1999/xhtml">More downloads</a>
</li>
</ul>
</div>
</div>

<div class="LinkList" xmlns="http://www.w3.org/1999/xhtml">
<div class="LinkListTitle">Support</div>
<div class="LinkListDescription"></div>
<div class="Links">
<ul class="LinkColumn" style="width: 100%">
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280308" id="Column372858" xmlns="http://www.w3.org/1999/xhtml">Forums<span class="Apple-tab-span" style="white-space:pre;" xmlns=""></span></a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280309" id="Column372859" xmlns="http://www.w3.org/1999/xhtml">More support options</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div data-fragmentName="Column4" id="Fragment_Column4" xmlns="http://www.w3.org/1999/xhtml">
<div class="ListOfLinkLists">

<div class="LinkList" xmlns="http://www.w3.org/1999/xhtml">
<div class="LinkListTitle">Essentials</div>
<div class="LinkListDescription"></div>
<div class="Links">
<ul class="LinkColumn" style="width: 100%">
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=281903" id="Column472860" xmlns="http://www.w3.org/1999/xhtml">Feature guide</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=285611" id="Column472861" xmlns="http://www.w3.org/1999/xhtml">Getting started</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280299" id="Column472862" xmlns="http://www.w3.org/1999/xhtml">Design guidance</a>
</li>
<li>
<a href="http://www.microsoft.com/click/services/Redirect2.ashx?CREAC=300098218" id="Column4728_63" xmlns="http://www.w3.org/1999/xhtml">Developer accounts</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=285613" id="Column472864" xmlns="http://www.w3.org/1999/xhtml">App certification</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=313905" id="Column472865" xmlns="http://www.w3.org/1999/xhtml">Featured partners</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=313906" id="Column472866" xmlns="http://www.w3.org/1999/xhtml">One Dev Minute videos</a>
</li>
</ul>
</div>
</div>

<div class="LinkList" xmlns="http://www.w3.org/1999/xhtml">
<div class="LinkListTitle">Stay connected</div>
<div class="LinkListDescription"></div>
<div class="Links">
<ul class="LinkColumn" style="width: 100%">
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=280304" id="Column472854" xmlns="http://www.w3.org/1999/xhtml">Microsoft events</a>
</li>
<li>
<a href="http://go.microsoft.com/fwlink/p/?linkid=313895" id="Column472855" xmlns="http://www.w3.org/1999/xhtml">Windows App Builder Blog</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>

<div id="baseFooterLogos">
<span class="microsoftlogo" title="Microsoft"></span>
<a href="http://msdn.microsoft.com/en-us" class="msdnlogo" title="MSDN"></a>
</div>

<div id="baseFooter">
<div id="FooterCopyright">© 2013 Microsoft</div>
<div data-fragmentName="BaseFooterContent" id="Fragment_BaseFooterContent" xmlns="http://www.w3.org/1999/xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<div xmlns:mtps="http://msdn2.microsoft.com/mtps">
<div data-fragmentName="BaseFooter" id="Fragment_BaseFooter">

<div class="LinkList">
<div class="LinkListDescription"></div>
<div class="Links">
<ul class="LinkColumn horizontal">
<li>
<a href="http://msdn.microsoft.com/windows/apps/cc300389" id="BaseFooter353511" xmlns="http://www.w3.org/1999/xhtml">Terms of Use</a>
</li>
<li>
<a href="http://www.microsoft.com/library/toolbar/3.0/trademarks/en-us.mspx" id="BaseFooter353512" xmlns="http://www.w3.org/1999/xhtml">Trademarks</a>
</li>
<li>
<a href="http://www.microsoft.com/info/privacy.mspx" id="BaseFooter353513" xmlns="http://www.w3.org/1999/xhtml">Privacy and Cookies</a>
</li>
</ul>
</div>
</div>
</div>
<div data-fragmentName="HelloText" id="Fragment_HelloText">Hello from Seattle.
</div>
</div>
</div>
</div>
<form class="selectLocale" id="selectLocaleForm" action="http://msdn.microsoft.com/en-us/windows/apps/SelectLocale">
<input type="hidden" name="fromPage" value="http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fwindows%2fapps%2fdn263169.aspx" />
<a href="#" onclick="$('#selectLocaleForm').submit();return false;" title="Change your language">United States (English)</a>
</form>


<div class="clear"></div>
</div>
</div>
</div>


<div class="footerPrintView">
<div class="footerCopyrightPrintView">© 2013 Microsoft. All rights reserved.</div>
</div>







<input id="MtpsDevice" type="hidden" value="Default" />


<!CDATA[ Third party scripts and code linked to or referenced from this website are licensed to you by the parties that own such code, not by Microsoft. See ASP.NET Ajax CDN Terms of Use – http://www.asp.net/ajaxlibrary/CDN.ashx.]>












<noscript><div><img alt="DCSIMG" id="Img1" width="1" height="1" src="http://m.webtrends.com/dcsmgru7m99k7mqmgrhudo0k8_8c6m/njs.gif?dcsuri=/nojavascript&amp;WT.js=No" /></div></noscript>









<noscript>
<a href="http://www.omniture.com" title="Web Analytics">
<img src="//msstonojsmsdn.112.2o7.net/b/ss/msstonojsmsdn/1/H.20.2--NS/0" height="1" width="1" border="0" alt="" />
</a>
</noscript>




<div id="globalRequestVerification">
<input name="RequestVerificationToken" type="hidden" value="FJpiJ3CveNE9GkMm947F7WEeA4F6I7cDvDD47soCTeKMUoQwz3RHYc59KUaIBHCmpQ6Xzj488gAyuBnUDtLfDF2VzxD-NBpVCErgfmg82yGR79iLmt8nUvedlRo2-ji1PD2TLxqKpZ55EdnPy8-B01" />
</div>


</div>
<script type="text/javascript" class="mtps-injected">
/*<!CDATA[*/
(function(window,document){"use strict";function preload(scripts){for(var result=[
,script,e,i=0;i<scripts.length;i++)script=scriptsi,script.hasOwnProperty("url")&&(e=document.createElement("script"),e.src=script.url,script.throwaway=e),result.push(script);return result}function inject(scripts,index){var script,elem;if(index>=scripts.length){delete mtps.injectScripts;return}script=scriptsindex;elem=document.createElement("script");elem.className="mtps-injected";elem.async=!1;var isLoaded=!1,timeoutId=0,injectNextFnName="",injectNext=elem.onerror=function(){isLoaded||(isLoaded=!0,inject(scripts,index+1),window.clearTimeout(timeoutId),elem.onload=elem.onerror=elem.onreadystatechange=null,injectNextFnName&&delete mtps[injectNextFnName],elem.removeEventListener&&elem.removeEventListener("load",injectNext,!1))};elem.addEventListener?elem.addEventListener("load",injectNext,!1):elem.readyState==="uninitialized"?elem.onreadystatechange=function(){(this.readyState==="loaded"||this.readyState==="complete")&&injectNext()}:elem.onload=injectNext;script.hasOwnProperty("url")?(timeoutId=window.setTimeout(injectNext,12e4),elem.src=script.url):(injectNextFnName="_injectNextScript_"+index,mtpsinjectNextFnName=injectNext,timeoutId=window.setTimeout(injectNext,2e3),elem.text="try {\n"+script.txt+"\n} finally { MTPS."+injectNextFnName+" && MTPS."+injectNextFnName+"(); }");parent.appendChild(elem)}var mtps=window.MTPS||(window.MTPS={}),parent=document.getElementsByTagName("head")0;mtps.injectScripts=function(scripts){inject(preload(scripts),0)}})(window,document);
MTPS.injectScripts([
{ txt: "/**/\r\n(window.MTPS || (window.MTPS = {})).cdnDomains || (window.MTPS.cdnDomains = { \r\n\t\"image\": \"http://i.msdn.microsoft.com\", \r\n\t\"js\": \"http://i2.msdn.microsoft.com\", \r\n\t\"css\": \"http://i3.msdn.microsoft.com\"\r\n});\r\n/**/" },
{ url: "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js" },
{ txt: "//\n var literalNormalizedUrl = \u0027/en-us/library/windows/apps/dn263169(d=default,l=en-us,v=win.10).aspx\u0027;\n var wt_nvr_ru = \u0027WT_NVR_RU\u0027;\n var wt_fpcdom = \u0027.microsoft.com\u0027;\n var wt_domlist = \u0027msdn.microsoft.com\u0027;\n var wt_pathlist = \u0027\u0027;\n var wt_paramlist = \u0027DCSext.mtps_devcenter\u0027;\n var wt_siteid = \u0027MSDN\u0027;\n var gDomain = \u0027m.webtrends.com\u0027;\n var gDcsId = \u0027dcsmgru7m99k7mqmgrhudo0k8_8c6m\u0027;\n var gFpc = \u0027WT_FPC\u0027;\n\n\n\n if (document.cookie.indexOf(gFpc + \"=\") == -1) {\n var wtidJs = document.createElement(\"script\");\n wtidJs.src = \"//\" + gDomain + \"/\" + gDcsId + \"/wtid.js\";\n document.getElementsByTagName(\"head\")[0].appendChild(wtidJs);\n }\n\n\n\n var detectedLocale = \u0027en-us\u0027;\n var wtsp = \u0027msdn\u0027;\n var gTrackEvents = \u00270\u0027;\n//" },
{ txt: "/**/\n var omni_guid = \"58f6291f-0897-4056-91f8-0a6eeefc9660\";\n/**/" },
{ url: "http://i2.msdn.microsoft.com/Combined.js?resources=0:Utilities,0:Topic,1:webtrendsscript,2:omni_rsid_MSDN,3:SearchBox;/Areas/Epx/Content/Scripts:0,/Areas/Global/Content/Webtrends/resources:1,/Areas/Global/Content/Omniture/resources/MSDN:2,/Areas/Epx/Themes/Base/Content:3\u0026amp;hashKey=9B53C4D2FC34522DCF65A603244675F2" },
{ url: "http://i1.services.social.microsoft.com/search/Widgets/SearchBox.jss?boxid=HeaderSearchTextBox\u0026btnid=HeaderSearchButton\u0026brand=MSDN\u0026loc=en-us\u0026Refinement=180\u0026focusOnInit=false\u0026iroot=windows%2fapps\u0026emptyWatermark=true\u0026searchButtonTooltip=Search" },
{ url: "http://i2.msdn.microsoft.com/Combined.js?resources=0:Header,1:Toc,1:LibraryMemberFilter,1:Toc_Fixed,2:Rating,3:CodeSnippet,3:TopicNotInScope,3:CollapsibleArea,3:VersionSelector,3:SurveyBroker;/Areas/Epx/Themes/Windows/Content:0,/Areas/Epx/Library/Content:1,/Areas/Epx/Themes/Base/Content:2,/Areas/Epx/Content/Scripts:3\u0026amp;hashKey=E47A73D19ECEB15A6B493E945949318A" },
{ txt: "$(document).ready(function() {\n try {\n var token = $(\"#globalRequestVerification input[name=\u0027__RequestVerificationToken\u0027]\").clone();\n $(\"#siteFeedbackForm\").append(token);\n } catch(err) {\n \n }\n });" }
]);

/]]>/
</script></body>
</html>


Manage life cycle and state in a C++ Windows Store app (tutorial 2 of 4) (Windows Recommended Release