Tag: Hardware back button

AdControl, Animations and the Back Key

Sometimes things don’t make sense, but bugs in software hardly do. In a new Windows Phone application I am implementing a fully functional trial mode that displays advertisements using Microsoft’s AdControl. I made sure to use the latest version of the AdControl (which is version 6.1.320.0). On one of the pages inside the application has both a list and detail view. If an item is selected from a list, the list disappears in the center of the page and detail information zooms in, making use of a few animations. Since the detail view seems to be a new page (even though it isn’t), I am overriding default hardware Back button behavior. If details are displayed when the hardware Back button is pressed, the list view becomes visible again. If the list view was already visible, I simply navigate back to the previous page. All in all, this functionality is straight forward and it is not uncommon to reuse pages for different views.

However, when I was testing the application, I ran into some unexpected behavior. With the AdControl visible, the animation that was supposed to run on pressing the hardware Back button did not run smoothly and sometimes it didn’t run at all. However, with the AdControl not visible, the animation was always running nice and smoothly.

This code is executed on pressing the hardware Back button:

Switching from detail to list
  1. protected override void OnBackKeyPress(CancelEventArgs e)
  2. {
  3.     if (lastActiveListBox != null)
  4.     {
  5.         e.Cancel = true;
  6.         lastActiveListBox = null;
  7.         VisualStateManager.GoToState(this, "TreeListState", true);
  8.     }
  9.     base.OnBackKeyPress(e);
  10. }

The following video shows the application in action, once with an AdControl on the page and once without the AdControl:

The AdControl influences animations

Of course this behavior is unacceptable for my application, so the next step is trying to work around the issue. After investigating a bit further, it seems that the combination of showing an AdControl and triggering an animation on the hardware Back button causes the problem. For instance, executing exactly the same code, but this time when tapping on the title bar (after adding a tap event handler), always results in smooth animations, independent of displaying an AdControl.

  1. protected override void OnBackKeyPress(CancelEventArgs e)
  2. {
  3.     if (lastActiveListBox != null)
  4.     {
  5.         e.Cancel = true;
  6.         lastActiveListBox = null;
  7.         VisualStateManager.GoToState(this, "TreeListState", true);
  8.     }
  9.     base.OnBackKeyPress(e);
  10. }
  11.  
  12. private void ApplicationTitle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
  13. {
  14.     if (lastActiveListBox != null)
  15.     {
  16.         lastActiveListBox = null;
  17.         VisualStateManager.GoToState(this, "TreeListState", true);
  18.     }
  19. }

Triggering the animation on the hardware Back button still shows the same behavior though:

The AdControl in combination with the Back Key influences animations

I am not sure what causes the problem. However, the fact that the animation runs smoothly when triggered in another way got me thinking about a more or less acceptable work around with not too much additional code involved. Since starting the animation on another event got rid of my problem, I came to the following solution. When the hardware Back button is pressed in my detailed view, I start a Timer that expires immediately. In the Tick event of the timer I am simply stopping the timer and I am starting the animation. This resulted in the following code:

Start animation on timer tick
  1. private DispatcherTimer timer;
  2.  
  3. public TreePage()
  4. {
  5.     InitializeComponent();
  6.  
  7.     if (App.ShowAds)
  8.     {
  9.         timer = new DispatcherTimer();
  10.         timer.Tick += new EventHandler(timer_Tick);
  11.         ah = new AdHelper(AdPlaceHolder, true);
  12.         this.Loaded += new RoutedEventHandler(ah.PhoneApplicationPage_Loaded);
  13.     }
  14.  
  15. }
  16.  
  17. void timer_Tick(object sender, EventArgs e)
  18. {
  19.     timer.Stop();
  20.     VisualStateManager.GoToState(this, "TreeListState", true);
  21. }
  22.  
  23. protected override void OnBackKeyPress(CancelEventArgs e)
  24. {
  25.     if (lastActiveListBox != null)
  26.     {
  27.         e.Cancel = true;
  28.         lastActiveListBox = null;
  29.         if (App.ShowAds)
  30.         {
  31.             timer.Start();
  32.         }
  33.         else
  34.         {
  35.             VisualStateManager.GoToState(this, "TreeListState", true);
  36.         }
  37.     }
  38.     base.OnBackKeyPress(e);
  39. }

This time the animation displays smoothly, independent of the presence or absence of an AdControl as is shown in the following video:

Animations are smooth when started on a Timer Tick

It would have been great if I could have explained why the AdControl in combination with the hardware Back button influences animations. For right now I assume this is a bug in the AdControl. At least, the work around shown in the blog entry is relatively simple and is not a big influence on either the memory footprint or the performance of your Windows Phone application. When a new version of the AdControl is available, I will make sure to test the identical scenario again and update this blog entry when results have changed.