views:

663

answers:

4

The background of the app is still there when i push the back button. Does anyone know what i can do, to prevent this? If i use Application.Exit(), it works.

thanks in advance love stackoverflow :)

A: 

I'm not sure exactly what you mean by the 'back button', but the default behavior in windows mobile for all apps is that the "X" button on the app that would be the "close" button in standard Windows, actually behaves like "minimize" and moves the app to the background.

To really "close" the application, you would normally go into the windows mobile task manager and end the application.

rally25rs
On many WM devices, in addition to the 'OK' button there is also a 'Back' button, symbolized by an arrow. Normal behavior, I believe, is that it immediately pops the user to the Today screen. I think this is the button he's talking about. I'm not sure how to alter or mask the button's behavior though, as it's usually not recognized by a KeyDown event or anything.
Matt G.
ooooh, a 'back' hardware button... that makes more sense. Have a Samsung Omnia (touch screen) myself, so I wasn't thinking about any hardware buttons. Sorry!
rally25rs
A: 

You can use the native function AllKeys and set the Form.KeyPreview to true to receive KeyDown events when the user presses hardware keys. Add a KeyDown event handler to your form, put a breakpoint in the KeyDown handler and press the "back" button and examine the value of KeyEventArgs.KeyCode. You can set KeyEventArgs.Handled to true to prevent the hardware keypress from being process by Windows Mobile.

Here is the PInvoke information:

[DllImport("coredll.dll", SetLastError = true)]
static extern bool AllKeys(bool bAllKeys);

Call AllKeys(true) to receive KeyDown events for hardware keys. Call AllKeys(false) to stop receiving KeyDown events for hardware keys. There is no official documentation for this function, but it has existed since Pocket PC 2003. See here for more information.

Trevor Balcom
+1  A: 

The back button is a Windows Mobile Standard (smartphone) feature that is typically not present in Windows Mobile Professional or Classic (pocket pc) devices.

At the native OS level the following MSDN article titled "The Back Button and Other Interesting Buttons" describes the default behavior and how to override or change it.

Within the .NET CF environment take a look at the "How to: Override the Smartphone Back Key" article available on MSDN. For example, within a form class you should be able to do something like the following.

// Connect an event handler to the KeyPress event
this.KeyPress += new KeyPressEventHandler(OnKeyPress);

private void OnKeyPress(object sender, KeyPressEventArgs ke)
{
    // Determine if ESC key value is pressed.
    if (ke.KeyChar == (Char)Keys.Escape)
    {
        // Handle the event to provide functionality.
        ke.Handled = true;

        // Add your event handling code here.
        MessageBox.Show("Back key was pressed.");
    }
}
Christopher Fairbairn
A: 

In Windows Mobile, you normally do not end processes. When you close the application with the [X] button, or when you go to the home screen, it is common to leave the app running.

An app on Windows Mobile does not take very much more memory, since it is not copied from harddisk into ram. It just stays on the same RAM spot, only now it is "active". Naturally your app should behave good, by taking as less resources as possible when it does not have focus.

If you do not want this behavior, you could exit the application ( Application.Exit() ), when your form looses focus. You can catch this easily with the LostFocus event.

Could it be that the "back-button" on my device is the one with the red phone printed on?

GvS