Rg.Plugins.Popup on device back button click in Xamarin Forms - xamarin.forms

Once the Rg.Plugin.Popup is displayed, with
PopupNavigation.Instance.PushAsync(new PopupPage());
Once its displayed, and on pressing Device Back button. screen getting hanged.
No OnBackPressed in declared in the code.

Once its displayed, and on pressing Device Back button. screen getting hanged. No OnBackPressed in declared in the code.
From this article, for Android back button to work with the plugin, you should invoke Rg.Plugins.Popup.Popup.SendBackPressed in your MainActivity in override method OnBackPressed.
public async override void OnBackPressed()
{
if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
{
// Do something if there are some pages in the `PopupStack`
await PopupNavigation.Instance.PopAsync();
}
else
{
// Do something if there are not any pages in the `PopupStack`
}
}

Related

How to navigate to page when on resume is called in app.xaml.cs for mvvm in xamarin forms app

I have an issue with navigation. When I click home screen button of device and get back to app I get app homescreen instead of pin page. Ideally it should show pin page and its working fine with back button of device.
OnStart() method has navigationasync but the same is not working with OnResume() method.
Do I have to go to each of the Platform project cs file and add the navigation there like for Android OnRestart()/OnResume() method?
If anyone knows the solution please let me know
Most commonly when writing your Xamarin Application with Prism you will have something like:
protected override void OnInitialized()
{
NavigationService.NavigateAsync("SomePage");
}
OnInitialized is called each time the App's ctor is invoked. This is an important consideration here because this means that any time that the native platform tombstones the app in the background or otherwise refreshes the app by calling OnCreate in your MainActivity or FinishedLaunching in your AppDelegate, then OnInitialized will be invoked resetting your App's Navigation stack to SomePage.
You can however override the OnStart/OnResume in PrismApplication and use whatever business logic you need to determine where to navigate and how you might want to restore your application.
public override void OnStart()
{
NavigationService.NavigateAsync("MainPage");
}
public override void OnResume()
{
if(someCondition)
{
NavigationService.NavigateAsync("SomePage");
}
else
{
NavigationService.NavigateAsync("AnotherPage");
}
}

Xamarin forms: Pop up page not closing when click device back button

I am using Rg.Plugins.Popup NuGet package for showing pop up page. When clicking the android device back button the pop-up page is not hiding, it only closes the content pages. I am working on a .Net standard project.
Versions:
Rg.Plugins.Popup - 1.1.5.180
Xamarin Forms - 3.3.0.967583
Pop-up Page Codes
protected override bool OnBackButtonPressed()
{
return base.OnBackButtonPressed();
}
protected override bool OnBackgroundClicked()
{
return base.OnBackgroundClicked();
}
BackgroundClicked closing the pop-up page, but the device back button pressed not closing the pop-up page.
For Android back button issues handle on 'OnBackPressed',
public override void OnBackPressed()
{
if (Rg.Plugins.Popup.Popup.SendBackPressed(base.OnBackPressed))
{
// Do something if there are some pages in the `PopupStack`
}
else
{
// Do something if there are not any pages in the `PopupStack`
}
}

Xamarin.Forms (iOS) MessagingCenter message from AppDelegate is not delivered

I have next goal: my app receives push notifications (FCM) and I need to handle user manipulations with them in all possible scenarios.
When I receive foreground push and handle it in WillPresentNotification - everything works as expected.
When I sent app to the background mode (clicking home button) and receive a push, click on that push at Notification Center I have next code in DidReceiveNotificationResponse method:
MessagingCenter.Send<object, PushNotificationObject>(this, "PushNavigationToRootPage", push);
So it just sends a message to the RootPage, which is subscribed/unsubscribed as below:
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Unsubscribe<object, PushNotificationObject>(this, "PushNavigationToRootPage");
MessagingCenter.Subscribe<object, PushNotificationObject>(this, "PushNavigationToRootPage", (sender, _push) => { ... });
}
protected override void OnDisappearing()
{
MessagingCenter.Unsubscribe<object, PushNotificationObject>(this, "PushNavigationToRootPage");
base.OnDisappearing();
}
enter code here
It also works! BUT!
If I completely close the app (swipe it off from App Switcher) and then click on the received push - it will only Send a message (from the code above), but will never reach code which is placed inside Subscribe of a RootPage.
Any ideas of what is wrong?
Thanks in advance!

How to Handle Back Button in Persistent Search Library

In my App i am Using Navigation Drawer and Persistent Search Library in Action
bar https://github.com/KieronQuinn/PersistentSearch
So when i am in my Home Activity and Search View is not Shown and i press back Button
App is Exit Normally (No Issue)
But when Search View is Open and i Press back button there is an Exception Occurs
So i want to know how to handle Back Button In Persistent Search Library
Here is the Exception Details
Exception image
I figure out the way of handling this as
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (this.search.isActivated()) {
closeSearch();
}
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
But Same Exception Occures
Any help will be Appreciated
Put this code in your activity to handle backpress event for search view
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == 4 && binding.appBrLt.searchbox.getVisibility() == View.VISIBLE) {
//here write the code
return true;
} else {
return super.dispatchKeyEvent(e);
}
}

FreshMvvm - Closing the child page of a page in FreshTabbedNavigationContainer

I have set up a FreshTabbedNavigationContainer with 5 pages.
Within the 5th tabbed page which is a SettingsPage where there is a way to display and AboutPage that is displayed via the SettingsPageModel like this
public Command AboutCommand
{
get
{
return new Command(() =>
{
CoreMethods.PushPageModel<AboutPageModel>();
});
}
}
When the user navigates away by selecting another tab, this other tab is displayed correctly.
If the user selects the SettingsPage via the tab, then the child AboutPage is automatically displayed.
I want to remove AboutPage from the navigation stack when another tab page is selected.
I have tried this in the AboutPage.xaml.cs
protected override void OnDisappearing()
{
base.OnDisappearing();
((AboutPageModel)BindingContext).CoreMethods.RemoveFromNavigation();
}
This works BUT if the back button is pressed on the app when in the AboutPage then it has already been removed from the navigation stack, and the app crashes.
How can I check if a PageModel is still in the navigation stack?
I have managed to resolve the problem :)
In the App.xaml.cs file in the App constructor where I create the FreshTabbedNavigationContainer, after setting the MainPage
MainPage = tabbedNavigation;
I then add the following code
tabbedNavigation.CurrentPageChanged += (sender, e) => {
tabbedNavigation.PopToRoot();
};
It works perfectly.

Resources