Home > Windows Phone > How to crash other WP7 applications?

How to crash other WP7 applications?

Do you know what is easiest way to crash someone’s WP7 application? Just quickly double-tap it’s “Rate my app” link or button. Marketplace Review page will display, but when you go back to app, it will disappear because it crashed.

Of course some app’s has this issue properly resolved, most not, but what happens when you double tap button or link that starts any kind of WP7 Launchers? Simply, your app will try twice to launch task, but second launch will throw “Navigation is not allowed when the task is not in the foreground” exception, because your app is not anymore active on phone.

Here is example. Create new Windows Phone application, uncomment ApplicationBar section at the end of MainPage.xaml and leave one ApplicationBarIconButton:

<phone:PhoneApplicationPage.ApplicationBar>
 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
 <shell:ApplicationBarIconButton IconUri="/Home.png" Text="navigate"/>
 </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar> 

In MainPage.xaml.cs add following code:

ApplicationBarIconButton appBarButton;

public MainPage()
{
    InitializeComponent();
    appBarButton = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    appBarButton.Click += new EventHandler(appBarButton_Click);
}

private void appBarButton_Click(object sender, EventArgs e)
{
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

This app will launch the web browser application and display the Bing web site. Start app and double-tap application bar icon button. Following unhandled exception will occur:

appbarcrash

This will happen in most situation where you are trying to start any of available launchers such as Web Browser Task, Share Link Task, Marketplace Review Task, etc. There are several ways to handle those situations:

1. Add task.Show() method in try/catch block:

private void appBarButton_Click(object sender, EventArgs e)
{
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    try {
        task.Show();
    }
    catch {}
}

2. Disable button on first click:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    appBarButton.Click += new EventHandler(appBarButton_Click);
    if ( e.NavigationMode == NavigationMode.Back )
        appBarButton.IsEnabled = true;
}

private void appBarButton_Click(object sender, EventArgs e)
{
    appBarButton.IsEnabled = false;
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

This will disable button, so second click won’t happen, but you also have to re-enable button when user goes back to this page.

3. Remove Click handler:

private void appBarButton_Click(object sender, EventArgs e)
{
    appBarButton.Click -= new EventHandler(appBarButton_Click);
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

Happy coding!

About these ads
  1. Martin
    February 21, 2012 at 1:51 pm

    Oh yeah, I fixed this in my app several months ago – I use as I call it “Safe handler”, which captures all unwanted double tap exceptions :)
    SmsComposeTask composeSMS = new SmsComposeTask
    {
    Body = message,
    To = number
    };
    SafeShow(composeSMS.Show);

    public static void SafeShow(Action action)
    {
    try
    {
    action();
    }
    catch (InvalidOperationException e)
    {
    // TODO log
    // catch possible Navigation exception for doubletaps
    }
    }

  2. Lauren Glenn
    February 22, 2012 at 8:13 pm

    It’s always good practice to have a TRY/CATCH when sending apps out into the world. People don’t like when programs don’t work but they really don’t like it when it crashes. It also makes you look bad when you don’t child proof the application.

  3. MattS
    July 20, 2012 at 8:22 pm

    I got a an address button which launches the bing map and also an Ad control at the top of the page.
    When I click the address button and before it launches, quickly click on the Ad, the app crashes. Is there a workaround for this?

    Note: When I click the Ad first and then the button nothing bad happens :)

  1. February 22, 2012 at 11:49 am
  2. February 26, 2012 at 1:05 pm
  3. February 28, 2012 at 6:26 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: