How to handle back button in windows phone 7? - windows-phone-7

I have an application, the main page contains few functions. To explain in detail - I have save, color palette button in my main page. When any of these buttons are clicked, save pop up or color palette appears. How to handle back button of the device, when color palette or save pop up is opened. When back button is pressed at these scenario it should just make them invisible and stay on the main page. When nothing is being performed in the main page, then it should come out of the app. I tried to make their visibility collapsed on back button press. But it still is coming out of the application.
Please, guide me in this. Thanks in advance.

Override PhoneApplicationPage.OnBackKeyPress and then set CancelEventArgs.Cancel to true if you want to stop it from actually going back.
protected override void OnBackKeyPress(CancelEventArgs args)
{
if (PanelIsShowing)
{
HidePanel();
args.Cancel = true;
}
}

The back button behaves as is intended by Microsoft.
If you change its behavior you risk your application not being certified for the Marketplace.
If you want the back button to close the popups, turn the popups into pages so the back button navigates back to the main page..

You need to use
protected override void OnBackKeyPress(CancelEventArgs e)
{
if (_popup.IsOpen)
{
_popup.IsOpen= false;
e.Cancel = true;
}
else
{
base.OnBackKeyPress(e);
}
}
That should do the trick.

Related

Detect Back Arrow Press Of The NavigationPage in Xamarin Forms

Is there any way to detect the press of the back button of the Navigation Page in Xamarin forms?
You can override your navigation page "OnBackButtonPressed" method:
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (await DisplayAlert("Exit?", "Are you sure you want to exit from this page?", "Yes", "No"))
{
base.OnBackButtonPressed();
await App.Navigation.PopAsync();
}
});
return true;
}
If you are using the shell, you can override the Shell's OnNavigating event:
void OnNavigating(object sender, ShellNavigatingEventArgs e)
{
// Cancel back navigation if data is unsaved
if (e.Source == ShellNavigationSource.Pop && !dataSaved)
{
e.Cancel();
}
}
Update:
OnBackButtonPressed event will get fired ONLY on Android when user press the Hardware back button.
Seems like you are more interested to implement when any page get disappeared you want to do something!
In that case:
You have the page's two methods -
protected override void OnAppearing()
{
base.OnAppearing();
Console.WriteLine("Hey, Im coming to your screen");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Console.WriteLine("Hey, Im going from your screen");
}
You can override those 2 methods on any page to track when they appear and disappear.
Recent updates to Xamarin forms mean you can now do this in an application made with Shell Navigation for navigation back arrow on both platforms.
Use the Shell.SetBackButtonBehavior method, for example running this code in the constructor of your page object will allow the back navigation to take place only when the bound viewmodel is not busy:
Shell.SetBackButtonBehavior(this, new BackButtonBehavior
{
Command = new Command(async() =>
{
if (ViewModel.IsNotBusy)
{
await Shell.Current.Navigation.PopAsync();
}
})
});
In the body of the Command you can do whatever you need to do when you are intercepting the click of the back button.
Note that this will affect only the navigation back button, not the Android hardware back button - that will need handling separately as per the answers above. You could write a shared method called from both the back button pressed override and the command on shell back button behaviour places to share the logic.
You must override native navigationbar button behavior with custom renderer. OnBackButtonPressed triggers only physical device button. You can read good article how to achive this here

Can we override NAVIGATION BACK BUTTON press in Xamarin.forms?

Can we override navigation back button pressed in Xamarin.forms?
I have one navigation back button and the one save button in navigation bar.Save button hits the web service and saves in asynchronous way. While saving although i used progressing bar, navigation back button can be pressed and hence the app crashes due to index out of range exception on navigation stack.I tried using OnDisappearing() , did not work. I wanna cancel the PopUpAsync(),if the save is not done completely, but failed to achieve that. Is there any solution for this scenario? Can we override the navigation back button press event using any custom renderer ?
For controlling the back button to do what I want, I used this method in Xamarin:
public override bool OnKeyDown(Keycode HWkeyCode, KeyEvent e)
{
if (HWkeyCode == Keycode.Back)
{
StartActivity(typeof(FrontPageActivity));
return true;
}
return false;
}

programmatically closing application on back key pressed event WP7

I'm trying to close my Wp7 app when the back is pressed in emulator.I have tried the code on that page from which I would close the whole app.the code is given below:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
if (NavigationService.CanGoBack)
{
while (NavigationService.RemoveBackEntry() != null)
{
NavigationService.RemoveBackEntry();
}
}
}
Help needed !
Thanks in advance!
First of all, There is no way to close wp7 application programatically.
And to do this you need not override back key press. When in the first page(ideally the home page or main page), pressing back key automatically closes the application.

How to handle the back button on Windows Phone 7

On the windows phone 7 emulator, when the hardware back button is pressed, the default behaviour is for it to close your current application. I want to override this default behaviour so that it navigates to the previous page in my application.
After some research, it seems it should be possible to do this by overriding the OnBackKeyPress method, like so:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
// do some stuff ...
// cancel the navigation
e.Cancel = true;
}
However, clicking the back button still closes my application. Putting a breakpoint on the above method reveals that it is never called. I have another breakpoint on my application exit code, and this breakpoint is hit.
Is there something else I need to do to intercept the back button?
It would appear that it's not possible to override the OnBackKeyPress method to intercept the back key unless you use the Navigate method to move between pages in your application.
My previous method of navigation was to change the root visual, like:
App.Current.RootVisual = new MyPage();
This meant I could keep all my pages in memory so I didn't need to cache the data stored on them (some of the data is collected over the net).
Now it seems I need to actually use the Navigate method on the page frame, which creates a new instance of the page I'm navigating to.
(App.Current.RootVisual as PhoneApplicationFrame).Navigate(
new Uri("/MyPage.xaml", UriKind.Relative));
Once I started navigating using this method, I could then override the back button handling in the way described in my question...
If you don't want the default back key behavior, set Cancel = true in the CancelEventArgs parameter in OnBackKeyPress. In my page, I've overridden the back button to close a web browser control instead of navigating back.
protected override void OnBackKeyPress(CancelEventArgs e)
{
if (Browser.Visibility == Visibility.Visible)
{
Browser.Visibility = Visibility.Collapsed;
e.Cancel = true;
}
}
I was able to use this technique to do what I wanted, which is to prevent back navigation while hiding a control that slides in and out of the window. By default, the control's visibility is collapsed. Storyboards are used to control when it becomes visible or collapsed. In XAML, inside the Storyboard:
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ControlScroller" Storyboard.TargetProperty="(UIElement.Visibility)">
<ObjectAnimationUsingKeyFrames.KeyFrames>
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames.KeyFrames>
Then in the page's code:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if(ControlScroller.Visibility == Visibility.Visible && StoryboardHideControlSlider.GetCurrentState() != ClockState.Active)
{
StoryboardHideControlSlider.Begin();
ContentGrid.IsHitTestVisible = true;
e.Cancel = true;
}
}
Note: In the Storyboard that hides the ContentScroller (which is a grid), the KeyTime is set to "00:00:01" because I want it to remain visible while it is sliding (and fading) out of view.
Note 2: The reason StoryboardHideControlSlider.GetCurrentState() != ClockState.Active is included in the if statement is because if the user hits the back button twice and the Storyboard hasn't completed it will run again. This prevents the backbutton cancelling navigation back to the previous page. So in other words, if the Storyboard is active, the code "knows" that the user has already initiated hiding it and intends to navigate back to the previous page. (Well, at least that's behavior they're going to get...and they won't see the animation twice)!

Sticky button in GWT

can i create a button in gwt that onClick stays pressed and if it looks pressed onClick releases it?
and the button has a different styles in each state?
any idea?
That sounds like a ToggleButton.
This is a pretty basic approach that should give you the toggle that you want. Basically the first click will put a 'clicked style' that you've created that will give the thing the look you want for when it looks pressed. Clicking it a second time will revert back to your normal button style so it will look not pressed again.
final Button button = new Button();
button.setStyleName("NormalButtonStyle");
button.addClickHandler( new ClickHandler() {
private boolean clickedStyle = false;
public void onClick( final ClickEvent clickEvent ){
clickedStyle = !clickedStyle;
if( clickedStyle ){
button.setStyleName("ClickedButtonStyle");
}
else {
button.setStyleName("NormalButtonStyle");
}
}
});

Resources