Posted by : Sudhir Chekuri Monday, 30 September 2013

There are three possible states for every app in their life cycle. They are Running, Suspended, Termination.

When an app is launched after splash screen it will be in running state, if user moves to other app then that app state will be suspended.
If user open that old app which is in suspended state it will resume back to running state.
In suspended state the app is not running but snapshot is there in memory.
The suspended apps will be terminated automatically without any message by the operating system if the memory is running out. There is no event to handle the termination but we have event executed when app is going to suspended state to store information. If app is terminated it is no more exists.

Launched -> Resume -> Suspend -> Terminate

Activation

Launched is first time app is opened.
Activation is bringing app front from suspended state.
An app can be activated in lot of ways like using tiles, secondary tiles(direct link for an app page), toast notification(swipes in from right side),...

onactivation event is available which also had a parameter that says type of activation.

State management guidelines

  1. Resume as user left it.
  2. Start fresh as a long period of time elapsed.
  3. Save data when suspending (5 seconds).
  4. Release exclusive resources.
There are two type of data. They are state data and application data.

Steps to create an example to save data when app is suspended and loading data when it is activated

Create a new blank object.
Drag and drop a textbox named as textbox1 to the design page.

The c# and xaml code of this example is given below.

Csharp code in mainpage

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace App1
{
   
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Loaded += MainPage_Loaded;
            App.Current.Suspending += Current_Suspending;
        }

        private void Current_Suspending(object sender, 

Windows.ApplicationModel.SuspendingEventArgs e)
        {
            Windows.Storage.ApplicationData.Current.RoamingSettings.Values

["MyData"] = textbox1.Text;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!

Windows.Storage.ApplicationData.Current.RoamingSettings.Values.ContainsKey

("MyData"))
            { textbox1.Text = "Default_Value"; }
            else
            {
                var _value = 

Windows.Storage.ApplicationData.Current.RoamingSettings.Values["MyData"];
                if (_value != null)
                { textbox1.Text = _value.ToString(); }
            }
        }
    }
}

xaml code:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="Black">
        <TextBox x:Name="textbox1" Width="200" HorizontalAlignment="Left" 

Margin="124,136,0,0"   VerticalAlignment="Top"/>
      </Grid>
</Page>


Add a menu bar - view- toolbars - debug location which gives options like suspend, resume and 'suspend and shutdown'

Launch the app and enter some text in the textbox.
When the app is suspended the value inside the textbox will be saved in roaming settings. The saved data will be loaded in textbox on mainpage load.
Select suspend and shutdown after execution to terminate the app.


Example 2 to save data when app is suspended and displayed when app is activated

create a blank xaml c# windows store app and use the below code in mainpage.xaml.cs

A textblock and textbox should be added in the design.
launch the app and then keep it aside. after some time that app will automatically go into suspend state.
on suspension of app the current system time is saved in local settings and displayed back when the app is resumed.

CSharp code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using System.ServiceModel.Channels;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App2
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
ApplicationDataContainer settings;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
Application.Current.Suspending += Current_Suspending;
Application.Current.Resuming += Current_Resuming;
settings = ApplicationData.Current.LocalSettings;
}

void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
settings.Values["suspendedDateTime"] = DateTime.Now.ToString();
settings.Values["customTextValue"] = custom.Text;
}

void Current_Resuming(object sender, object e)
{
Msg.Text = "Resumed. Was suspended at\n\n" + settings.Values["suspendedDateTime"];
custom.Text = settings.Values["customTextValue"].ToString();
}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
Application.Current.Resuming -= Current_Resuming;
Application.Current.Suspending += Current_Suspending;
}
}
}




Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

Blog Archive

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -