Derik Whittaker

Syndication

News


How to determine if a View is loaded off of the Backstack in WP7

One of the great features (also one which is not fully implemented in my opinion) is the backstack in WP7.  The idea of the back stack is that when you navigate away from a page that page is pushed to this stack so that when the user hits the back button (or it is done via code) the previous page can be reloaded off of the stack rather than being recreated.

This is both a powerful as well as clumsy feature of WP7.  I say it is clumsy because there is nothing built into the framework (as far as I can tell) which tells you on page load if the page is being newly constructed or if it is being rehydrated via back navigation. 

Because there are cases in our app that I want to know if the page is being accessed off of the backstack vs being newly created I set out to determine how I could build in the ability to detect this.  After a bit of research on google I was not able to find any solutions so I built my own.  Please note, that this may not be the best solution (I am open to hear about better ones) but it does appear to work.

Before we dive into the logic used to determine how a view is being loaded we need to understand the some page life cycle events and the order in which they fire.

Life cycle events for a newly constructed page (forward navigation)

  • Page Constructor
  • Page Loaded event fired
  • Page Unloaded event fired (when you navigate away from the page)

Life cycle events for a page being rebuild off of the backstack (back navigation)

  • Page Constructor (this is NOT fired when being pulled off of the back stack as the page is not being created again)
  • Page Loaded event fired
  • Page Unloaded event fired (when you navigate away from the page)

 

Now that we understand the page life cycle events how do we determine how the page is being build?

1) Create an enumeration which helps us understand the life stage of the page

/// 
/// Enumeration to provide details on the view stage for a given view.
/// This is used to determine if a view is being newly constructed or if it has been
///     pulled off of the backstack
/// 
public enum ViewLifeStage
{
    /// 
    /// Will indicate that the view has been newly created.
    /// This will be set when the view has been navigated to via forward navigation
    /// 
    Constructed,        

    /// 
    /// Will indicate that the view has been reloaded off of the backstack
    /// This will be set when the view is navigated to via the back button/back stack
    /// 
    Rehydrated          
}

2) Set the initial  life stage in the constructor of the page (remember this is ONLY hit on forward navigation)

ViewsLifeStage = ViewLifeStage.Constructed;

3) Update the life stage when you unload the page

ViewsLifeStage = ViewLifeStage.Rehydrated;

4) Check the life stage when you load a page (if needed)

if ( ViewsLifeStage == ViewLifeStage.Rehydrated )
{
    // do something here
}

As you can see from the steps above there is a few lines of code you need to create, but nothing too much.  Again the above appears to work, but it may not be as elegant (oh and I am not sure I like the terms used in the enumeration, but that is a simply terminology change).  If there is a better/cleaner solution for this please let me know.

Till next time,


Posted 11-30-2010 6:46 AM by Derik Whittaker

[Advertisement]

Comments

dawmail333 wrote re: How to determine if a View is loaded off of the Backstack in WP7
on 12-01-2010 2:55 AM

Why don't you just use a boolean? :P

(isNew = True)

You don't have to like the terms then :P

jc wrote re: How to determine if a View is loaded off of the Backstack in WP7
on 12-01-2010 9:49 AM

Yup. Boolean would be for efficient then an enum

brandon wrote re: How to determine if a View is loaded off of the Backstack in WP7
on 12-01-2010 11:14 AM

Boo on enumeration-driven programming!

Welt wrote re: How to determine if a View is loaded off of the Backstack in WP7
on 12-01-2010 1:14 PM

Right, boolean would be the best option ;) You are probably confusing it with server technologies like ASP where the page is reconstructed every time regardless of GET or POST-back (for obvious performance reasons - it would be insane to store all information on the server-side with so many users). In WP7 there is only one client - the user of the device; that's why there is no need to reconstruct the page every time, and no need for built-in mechanism you describe.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)