shureman
shureman

Reputation: 51

Navigation in Application Activated Windows Phone 7 (Tombstoning)

Whenever my program is closed via tombstoning, when it is reactivated I would like the application to Navigate back to the start screen.

I would like to do something like this

private void Application_Activated(object sender, ActivatedEventArgs e) { NavigationService.Navigate(new Uri("/Start.xaml", UriKind.Relative));
}

but it doesn't work. Thanks, Shureman.

Upvotes: 5

Views: 3458

Answers (5)

Clinton Ward
Clinton Ward

Reputation: 2511

Dont listen to all these do gooders who quote guidlines yawn

try this

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    RootFrame.Navigated += RootFrame_Navigated;
}

void RootFrame_Navigated(object sender, NavigationEventArgs e)
{
    RootFrame.Navigated -= RootFrame_Navigated;
    RootFrame.Navigate(new Uri("/TestPage.xaml", UriKind.Relative));
}

Upvotes: 2

jchristof
jchristof

Reputation: 2834

We ran into the same issue converting a large legacy project and needed to take some liberties with the tomstoning. We're all relatively new to this platform so take this advice with a grain of salt. Our startup page is the SplashPage.xaml. We used the UriMapper to redirect from the last current source:

    private void Application_Activated(object sender, ActivatedEventArgs e) {
        IsTombstoned = ! e.IsApplicationInstancePreserved;

        if (IsTombstoned) {
            //the os wants to return to the last page, but we want it to restart to our splash page 
            RootFrame.UriMapper = new RestartUriMapper();
        }
    }

This redirects to the SplashPage.xaml and then we do another navigation to clear that last page that the OS wants to go to (might be unique to our navigation implementation.)

    protected override void OnNavigatedTo(NavigationEventArgs e) {
        base.OnNavigatedTo(e);

        if (NavigationContext.QueryString.ContainsKey("restart")) {
            var app = Application.Current as App;
            //a page redirect mapper was installed to get here from tombstone - reinstate the AssociationUriMapper now
            app.RootFrame.UriMapper = App.Root.AssociationUriMapper;

            //from tombstone, the last current nav source is still state, so force an initial navigation back to 
            //a new instance of this splash page and proceed start up from there
            App.PageNavigation.Navigate(@"/Pages/SplashPage.xaml?fromtomb=true");
        }
    }


class RestartUriMapper : UriMapperBase {
    Uri restartUri;

    public RestartUriMapper() {
        restartUri = new Uri(string.Format("/Pages/SplashPage.xaml?restart={0}", true.ToString()), UriKind.Relative);
    }

    public override Uri MapUri(Uri uri) {
        if (restartUri != null) {
            Uri nextPageUri = restartUri;
            restartUri = null;
            return nextPageUri;
        }

        return uri;
    }
}

Upvotes: 0

Matt Lacey
Matt Lacey

Reputation: 65586

That's not the generally accepted behaviour around tombstoning. The expectation is that the app should return exactly as it was when the user left. Remember that tombstoning may be a result of something other than a user initiated action within the app. For instance, as a user, I wouldn't want an app to forget all the information I've entered and return to a previous screen just because I answered a phone call.

If you really wanted to do this, how it could be done would depend on the structure of your application and the navigation hierarchy.

Your best bet would probably be to build your own navigation system.
If you wanted to use the built in back stack. Your Application_Activated event could set a global flag that all pages will pick up in their OnNavigatedTo event and then respond to by navigating backwards. This backwards navigation would likely be visible (if only briefly) to the user and create a less than desirable experience.

Update
It's now possible to do something like this using the Non-Linear Navigation Service.

Upvotes: 5

Henry C
Henry C

Reputation: 4801

As @Matt Lacey has said, this is almost definitely something you shouldn't do: and you will probably fall foul of the marketplace certification guidelines also:

5.2.3 Application Responsiveness After Being Deactivated

A Windows Phone application is deactivated when the user presses the Start button or if the device timeout causes the lock screen to engage. A Windows Phone application is also deactivated when it invokes a Launcher or a Chooser API. When activated, the application launch time must meet the requirements in Section 5.2.1.

Microsoft recommends that the application reestablishes the state of the application that the user experienced before the application was deactivated. For more information, see the Execution Model Overview for Windows Phone topic.

What sort of application are you making? It's hard to make a call on whether returning to the start screen is appropriate without knowing a bit more about it, or the context of the program.

Upvotes: 0

Jobi Joy
Jobi Joy

Reputation: 50048

I second Matt, that is not a recommended behavior by the MSFT guidelines. WP7 user will expect the app to be tomb stoned properly.

And if you are still stringent on doing this here is the way: Use NavigationService.GoBack() as many times as you navigated. Technically WP7 keeps all the page transition you already did in the system and you can program to go back to the home page. You might need to wait for the NavigationCompleted event then call the next GoBack() and call it untill NavigationService.CanGoBack is false , which will be your home page :)

Upvotes: 2

Related Questions