Charles Petzold www.charlespetzold.com Application Lifecycle and State Management.

Slides:



Advertisements
Similar presentations
Interaction Design: Visio
Advertisements

Microsoft Office 2007-Illustrated Introductory, Windows Vista Edition Windows XP Unit A.
DEVELOPING ICT SKILLS PART -TWO
Part 2: Manage app lifecycle and state (Windows Store apps using C#/VB and XAML) us/library/windows/apps/hh aspx
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Understanding Mobile App Development Concepts and Working with APIs Lesson 6.
SIVA PRASAD SANDIREDDY.  YzdS4.
The Web Warrior Guide to Web Design Technologies
Charles Petzold Marketplace Deployment.
Building Windows Phone Applications with Silverlight Nguyen Thanh Tung Project Manager - MISA JSC.
1 of 6 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
.NET Class 4 – Windows-based Application. WinForm Application Homogeny programming model. Rich class library Classes are shared by all.NET languages.
Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
A Guide to Oracle9i1 Creating an Integrated Database Application Chapter 8.
Format Scandisk Defragmentation Antivirus Compression Software
Portable Software. This program will explain what portable software is, how it can be used, and where it can be found. This is an advanced level technology.
Operating Systems Day 3. Changing Date & Time 1.Double click on digital clock on the notification area of a task bar (Click start button, Click control.
Using Microsoft Outlook: Basics. Objectives Guided Tour of Outlook –Identification –Views Basics –Contacts –Folders –Web Access Q&A.
Charles Petzold Navigation.
1 Chapter Overview Creating User and Computer Objects Maintaining User Accounts Creating User Profiles.
1 Chapter Overview Managing Data Storage Creating Dynamic Disks Implementing Storage Quotas Managing Compression and Encryption.
Windows Programming Using C# Windows Services, Serialization, and Isolated Storage.
Customizing Microsoft Project
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Configuring the MagicInfo Pro Display
Advanced User Guide to Outlook and all its features.
V 1.0 Programming III. Creation of additional windows Routed events.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Charles Petzold Launchers and Choosers.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 4 I Need a Tour Guide.
Microsoft Office Word 2013 Expert Microsoft Office Word 2013 Expert Courseware # 3251 Lesson 3: Customizing Document Elements.
File I/O 11_file_processing.ppt
Microsoft Tech Days 2012 Cheezia: Developing a Windows Phone XNA Game Rodrigo Barretto Software Engineer - MCPD on Windows Phone
1 The EDIT Program The Edit program is a full screen text editor that allows you to: Create text files Create text files Edit an existing text files Edit.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Microsoft Office Outlook 2013 Microsoft Office Outlook 2013 Courseware # 3252 Lesson 6: Organizing Information.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Object Oriented Software Development 10. Persistent Storage.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Module 14 Application Settings, State, and Life Cycle.
Styles, Dialog Boxes, and Menus. Styles Allow creation of a common format – placed in res/values/styles.xml – file name is incidental Can be applied.
3 4 private void saveButton_Click(object sender, RoutedEventArgs e) { saveText("jot.txt", jotTextBox.Text); }
Building Windows Phone Applications with Silverlight Jaime Rodriguez
private void Application_Launching(object sender, LaunchingEventArgs e) { } private void Application_Activated(object.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Operating System Concepts Three User Interfaces Command-line Job-Control Language (JCL) Graphical User Interface (GUI)
Chapter 3 I Need a Tour Guide (Introduction to Visual Basic 2010) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Lesson 6: Controlling Access to Local Hardware and Applications
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Programering af mobile enheder Windows Phone Uge 9 Part 1.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Chapter 2 – Introduction to Windows Operating System II Manipulating Windows GUI 1CMPF112 Computing Skills for Engineers.
Files.
Windows Phone 7 advanced services
Windows Phone Platform Integration Yochay Kiriaty
Using Multiple Forms.
Multi-host Internet Access Portal (MIAP) Enhancement Guide
Gathering User Input Event Handling Create Dynamic controls
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
Activities and Intents
Lecture Set 10 Windows Controls and Forms
by Santosh Reddy Vuppala
Ch07 生命週期(Life Cycle).
Presentation transcript:

Charles Petzold Application Lifecycle and State Management

Agenda Launching and closing Isolated storage Activation and deactivation Tombstoning Page state and application state Obscuration Idle detection

Phones run one app at a time –Apps not in foreground are closed or deactivated –Either way, process is terminated (usually) PhoneApplicationService fires lifecycle events –Launching and Closing –Activated and Deactivated State is not maintained when app is closed or deactivated unless you take steps to maintain it –Except in certain situations (more later) Application Lifecycle

Application is closed when: –Application's first page is displayed, and… –User presses the phone's Back button The only way to close an application! Process is terminated; all state is lost –One exception: if Back button is pressed quickly, WP7 may reconnect to still-running process –But don't count on it Launching and Closing

Launching and Closing Events // App.xaml <shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing"... /> // App.xaml.cs void Application_Launching(object sender, LaunchingEventArgs e) { } void Application_Closing(object sender, ClosingEventArgs e) { }

Every Windows phone has a Back button Page fires BackKeyPress events when the Back button is pressed From the UI Design and Interaction Guide for Windows Phone 7: Back Button Developers should only implement Back Button behaviors that navigate back or dismiss context menus or modal dialog boxes. All other implementations are prohibited.  

Prompting Before Exiting BackKeyPress += new EventHandler (OnBackKeyPress);. private void OnBackKeyPress(object sender, CancelEventArgs e) { MessageBoxResult result = MessageBox.Show("Are you sure?", "Confirm", MessageBoxButton.OKCancel); e.Cancel = !(result == MessageBoxResult.OK); }

demo Launching and Closing

System.IO.IsolatedStorage namespace contains classes that provide access to virtual file system –IsolatedStorageFile –IsolatedStorageException –IsolatedStorageSettings Stored in memory on the phone Permanently deleted if app is uninstalled Not subject to quotas Isolated Storage

Provides methods for: –Opening and closing isolated storage stores Also known as "isostores" –Creating, opening, copying, enumerating, moving, renaming, and deleting isolated storage files –Creating, opening, enumerating, moving, renaming, and deleting isolated storage folders AvailableFreeSpace property provides information about free space IsolatedStorageFile

IsolatedStorageFile method for accessing per- application stores GetUserStoreForApplication App 1 Store App 1 Store App 2 Store App 2 Store Marketplace App 1 App 2 Phone

Writing to Isolated Storage using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Settings.xml", FileMode.Create, store)) { using (StreamWriter writer = new StreamWriter(stream)) { // TODO: Write to stream with StreamWriter }

Reading from Isolated Storage using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { if (store.FileExists("Settings.xml")) { using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Settings.xml", FileMode.Open, store)) { using (StreamReader reader = new StreamReader(stream)) { // TODO: Read from stream with StreamReader }

Deleting an Isolated Storage File using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { if (store.FileExists("Settings.xml")) store.DeleteFile("Settings.xml"); }

Dictionary stored in isolated storage –Application scope –Accessed through IsolatedStorageSettings.ApplicationSettings property Simplifies task of storing user preferences and other simple settings –For example, language preferences Private to each application Application Settings

Using ApplicationSettings // Write to the ApplicationSettings dictionary IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; settings["Culture"] = "fr-fr"; // Read from the ApplicationSettings dictionary string culture; IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; if (settings.TryGetValue ("Culture", out culture)) HtmlPage.Window.Alert(culture);

Determining Available Space using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { Int64 available = store.AvailableFreeSpace;... }

Performance of CreateFile, OpenFile, and other methods degrades with large numbers of files Rule of thumb: Segregate files into folders, with no more than 128 files per folder –"How Many Files are Too Many Files?" Block reads and writes are more performant than lots of little reads and writes Isolated Storage Performance

Creating an Isolated Storage Folder using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { store.CreateDirectory("\XML Stuff"); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("\XML Stuff\Settings.xml", FileMode.Create, store)) {... }

demo Isolated Storage

Application is deactivated when: –User presses the phone's Start button –App launches a launcher or chooser –Device time-out locks the screen Unless you specify otherwise Deactivation usually means process is terminated –Notable exception: When certain choosers such as PhotoChooserTask are launched Termination means all state is lost Activation and Deactivation

Activated and Deactivated Events // App.xaml <shell:PhoneApplicationService Activated="Application_Activated" Deactivated="Application_Deactivated"... /> // App.xaml.cs void Application_Activated(object sender, ActivatedEventArgs e) { } void Application_Deactivated(object sender, DeactivatedEventArgs e) { }

demo Activation and Deactivation

Occurs when app is deactivated and terminated To restore app to same state when reactivated, persist state in application state or page state –Or use isolated storage if volume of data exceeds limits on application state and page state –Isolated storage is 30% to 50% slower Use page's OnNavigatedFrom and OnNavigatedTo methods to save and restore state App is allowed 10 seconds to tombstone data Tombstoning

Data store for tombstoning global data –Limited to approx. 1.5 MB per app Dictionary accessed through PhoneApplicationService.State property Available between Activated and Deactivated events, inclusive Data must be serializable Application State

Saving Application State void Application_Deactivated(object sender, DeactivatedEventArgs e) { PhoneApplicationService.Current.State["x1"] = x1; PhoneApplicationService.Current.State["y1"] = y1; PhoneApplicationService.Current.State["x2"] = x2; PhoneApplicationService.Current.State["y2"] = y2; }

Restoring Application State void Application_Activated(object sender, ActivatedEventArgs e) { if (PhoneApplicationService.Current.State.ContainsKey("x1")) x1 = (double)PhoneApplicationService.Current.State["x1"]; if (PhoneApplicationService.Current.State.ContainsKey("y1")) y1 = (double)PhoneApplicationService.Current.State["y1"]; if (PhoneApplicationService.Current.State.ContainsKey("x2")) x2 = (double)PhoneApplicationService.Current.State["x2"]; if (PhoneApplicationService.Current.State.ContainsKey("y2")) y2 = (double)PhoneApplicationService.Current.State["y2"]; }

Data store for tombstoning per-page data –Limited to 2 MB per page and 4 MB per app Dictionary accessed through PhoneApplicationPage.State property Available between OnNavigatedTo and OnNavigatedFrom methods, inclusive Data must be serializable Page State

Saving Page State protected override void OnNavigatedFrom(NavigationEventArgs e) { this.State["x1"] = x1; this.State["y1"] = y1; this.State["x2"] = x2; this.State["y2"] = y2; }

Restoring Page State protected override void OnNavigatedTo(NavigationEventArgs e) { if (this.State.ContainsKey("x1")) x1 = (double)this.State["x1"]; if (this.State.ContainsKey("y1")) y1 = (double)this.State["y1"]; if (this.State.ContainsKey("x2")) x2 = (double)this.State["x2"]; if (this.State.ContainsKey("y2")) y2 = (double)this.State["y2"]; }

demo Tombstoning

Tombstoning a ListBox Control protected override void OnNavigatedFrom(NavigationEventArgs e) { // Save the ListBox control's SelectedIndex in page state this.State["Index"] = ListBoxControl.SelectedIndex; } protected override void OnNavigatedTo(NavigationEventArgs e) { // Restore the ListBox control's SelectedIndex if (this.State.ContainsKey("Index")) ListBoxControl.SelectedIndex = (int)this.State["Index"]; }

Tombstoning a Pivot Control // This doesn't work protected override void OnNavigatedFrom(NavigationEventArgs e) { // Save the Pivot control's SelectedIndex in page state this.State["Index"] = PivotControl.SelectedIndex; } protected override void OnNavigatedTo(NavigationEventArgs e) { // Restore the Pivot control's SelectedIndex if (this.State.ContainsKey("Index")) PivotControl.SelectedIndex = (int)this.State["Index"]; }

Tombstoning a Pivot Control, Cont. // This works protected override void OnNavigatedFrom(NavigationEventArgs e) { // Save the Pivot control's SelectedIndex in page state this.State["Index"] = PivotControl.SelectedIndex; } // Handler for Pivot.Loaded event void OnPivotControlLoaded(object sender, RoutedEventArgs e) { // Restore the Pivot control's SelectedIndex if (this.State.ContainsKey("Index")) _index = (int)this.State["Index"]; }

Tombstoning a Panorama Control // This doesn't work protected override void OnNavigatedFrom(NavigationEventArgs e) { // Save the Panorama control's SelectedIndex in page state this.State["Index"] = PanoramaControl.SelectedIndex; } protected override void OnNavigatedTo(NavigationEventArgs e) { // Restore the Panorama control's SelectedIndex if (this.State.ContainsKey("Index")) PanoramaControl.SelectedIndex = (int)this.State["Index"]; }

Tombstoning a Panorama, Cont. // This works protected override void OnNavigatedFrom(NavigationEventArgs e) { // Save the Panorama control's SelectedIndex in page state this.State["Index"] = PanoramaControl.SelectedIndex; } protected override void OnNavigatedTo(NavigationEventArgs e) { // Restore the Panorama control's SelectedIndex if (this.State.ContainsKey("Index")) PanoramaControl.DefaultItem = PanoramaControl.Items[(int)State["Index"]]; }

demo Tombstoning Pivot Controls

Fired by PhoneApplicationFrame –Obscured – Shell comes to the foreground –Unobscured – App returns to the foreground Common causes: –Phone receives an incoming call –Screen times out and lock screen appears If app has disabled idle detection Some apps must handle these events to pass certification requirements Obscured and Unobscured Events

Handling Obscuration Events (Application.Current as App).RootFrame.Obscured += OnObscured; (Application.Current as App).RootFrame.Unobscured += OnUnobscured;. private void OnObscured(object sender, ObscuredEventArgs e) { // Application is obscured by shell (possibly an incoming call) VibrateController.Default.Stop(); // Sample action } private void OnUnobscured(object sender, EventArgs e) { // Application has returned to the foreground }

By default, apps are deactivated when screen locks Apps can continue running when screen locks by disabling ApplicationIdleDetectionMode –Primarily for apps that are slow to reactivate –Also for apps that need to run under lock (e.g., audio) –Obscured and Obscured events replace Activated and Deactivated events Apps can also disable UserIdleDetectionMode to prevent screen from locking automatically Idle Detection

Disabling Application Idle Detection // Allow the app to run when screen is locked. Once disabled, // ApplicationIdleDetectionMode cannot be reenabled while the // app is running. PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;

Disabling User Idle Detection // Prevent screen from locking while this app runs PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;

Detecting a Locked Screen (Application.Current as App).RootFrame.Obscured += OnObscured; (Application.Current as App).RootFrame.Unobscured += OnUnobscured;. private void OnObscured(object sender, ObscuredEventArgs e) { if (e.IsLocked) { // Screen is locked } private void OnUnobscured(object sender, EventArgs e) { // Screen is no longer locked }

demo Running Under a Locked Screen

Charles Petzold Questions?