I’d like to ask you about the best way of handling navigation between pages/activities on mobile platforms. To be more specific – about handling state in more complex apps, about handling ‘Back’ actions, etc…
At the beginning it’s worth to mention that I’m looking for a solution which will fit to MVVM approach (Windows Phone).
Of course first page of Google gives me some answers but in 99% of examples we have simple cases like (C# mixed with pseudocode):
public class ExampleViewModel : INavigationViewModel
{
public void Activate()
{
//load somehow saved state if it exists
if(savedState != null) { //assign previously saved state to current VM }
}
public void Deactivate()
{
//save view model state and it will be ok
}
}
So it looks pretty clear, right? Interface INavigationViewModel has defined two basic methods which simulate moving forward and back. During changing pages that methods are executed so we may think that everything will work ok.
Now let’s take a look at more realistic situation:
public class ExampleViewModel : INavigationViewModel
{
public void Activate(Dictionary<key,value> someParams)
{
//Activation, but from which case?
// 1. From previous page? Ok, we have someParams != null.
// 2. From 'back' button? Not good, someParams == null.
// 3. From hidden state? Not good...
// I think you get the point, right?
}
public void Deactivate()
{
//Deactivation types might be like:
// 1. Going to another page
// 2. Quick move forward and backward (yes, different than 1.)
// 3. Closing app from this moment (what about filled data?)
// 4. Full page activities (for eg. full screen datepicker)
// Maybe some other deactivation cases?
}
}
Some devs (like me now) are using static classes – ‘DataContainers’ to handle page state. Ok that’s good, but we have situations when under the same key there are unwanted or different values.
That’s not all – with many ‘activation cases’ we need to write many conditional statements. In more complex apps it’s nearly impossible to handle all variations of launching ViewModel.
In programming world we have many design patterns. I can’t believe that’s so few ideas (or maybe my research was not good enough) for this problem – navigation in mobile apps without writing methods with hundreds of cases.
It would be great to see something like Strategy Pattern with encapsulated cases, based on interfaces with really clean activation methods, etc… is this possible?
Include crucially required state information as url parameters/cookie-as-last-resort (assuming you probably loose the querystring portion of a request in some cases).
Much like you may have a controller/action url portion to your MVC/VM setup, you can include id
i.e. http://wherever/area/process/stage/essential_param1/2/3
This way you still can continue from where you need to relatively easily, and request any remaining parameters from the user or database.