0

I want to identify the page user is on when the app moves to background.

Reason : When the app enters foreground, I need to log it as a page view in telemetry. So, it is important for me to understand which page the user was on when app moved to background.

SRIRAM
  • 137
  • 1
  • 1
  • 7
  • 1
    Use your AppDelegate, there is a callback for this in it. But, try to [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – user9335240 Feb 14 '18 at 21:27

1 Answers1

0

When the app moves to background, it will trigger OnResignActivation and DidEnterBackground method, what you should do is that find the top viewcontroller in DidEnterBackground in AppDelegate.

String pageName;
public override void DidEnterBackground(UIApplication application)
{
    UIViewController controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
    while (controller.PresentedViewController != null)
    {
        controller = controller.PresentedViewController;
    }

    UIViewController c = findTopViewController(controller);
    pageName = c.Class.Name;
}

public UIViewController findTopViewController(UIViewController controller)
{
    if (controller is UINavigationController) {
        controller = (controller as UINavigationController).TopViewController;
        findTopViewController(controller);
    }
    else if (controller is UITabBarController)
    {
        controller = (controller as UITabBarController).SelectedViewController;
        findTopViewController(controller);
    }
    return controller;
}

Refer to this answer and comments under it.

ColeX
  • 14,062
  • 5
  • 43
  • 240