Richard
Richard

Reputation: 389

Universal app detect start focus lost

I am writing a universal app for WP8.1 I don't want it to suspend.

Some banking apps are like this. If a banking app is running and I click the back button or the start button, the app does not get suspended. It exits. I suppose the developers do not want bank account credentials hanging about in state data.

I am trying to do the same for my secure-messaging app. I can put an event on the back button:

Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

and inside this event handler call:

App.Current.Exit();

This works.

But how do I detect if the app loses focus from the user pressing the start button, search button or off button?

I have tried this event in the App class:

Window.Current.Activated += Current_Activated;

and this function, also in the App class:

protected override void OnActivated(IActivatedEventArgs args)

but neither of them are called.

Upvotes: 0

Views: 249

Answers (1)

Decade Moon
Decade Moon

Reputation: 34306

Like this:

public sealed partial class App : Application
{
    /* --- Omitted generated code here --- */

    protected override void OnWindowCreated(WindowCreatedEventArgs args)
    {
        args.Window.VisibilityChanged += Window_VisibilityChanged;
    }

    void Window_VisibilityChanged(object sender, VisibilityChangedEventArgs e)
    {
        if (!e.Visible)
            Exit();
    }
}

Upvotes: 1

Related Questions