I have implemented below navigation for my app which has login and logout feature.
How to block hardware Back button and re-login again. Below is the flow:
1) Login page is the MainPage when App launch
MainPage = new Login();
2) After successfully login, user will be navigated to MainMenu Page
NavigationPage NP = new NavigationPage(new MainMenu());
App.current.MainPage = Np;
In MainMenu:
1) How to I override the Hardware button for iOS and Android (but iOS has no Back Button in current ipad and iphone).
This is what I got so far to stop back button.
protected override bool OnBackButtonPressed()
{
base.OnBackButtonPressed();
return false
}
1a) How to detect if device is iOS and Android phone? Since iOS has no back Button, will onBackButtonPressed() apply to it?
1b) will putting return false before or after base.OnBackButtonPressed(), make a difference?
2) User logout
in the beginning: Login -> MainMenu: in MainMenu page, user click Logout button
void LogoutButton()
{
Navigate.PopModalAsync(new Login());
}
will this cause any problem since the first time login, MainPage is App.current.MainPage = Np;
Now what is the Mainpage = ?? when user click Logout button?
What happen when user login again? Which Navigation method I should use to go back to Login Page?
Thanks
Let me start by saying that asking multiple questions at once isn't really according to the StackOverflow guidelines. To answer your questions:
1) How to I override the Hardware button for iOS and Android (but iOS
has no Back Button in current ipad and iphone).
The method you're overriding does indeed stop the hardware back button. It does not however stop every method a user has to go back. What you could do instead is create a separate Activity for your login and decorate that with the following:
[Activity(Label = "MyApp",
Icon = "#drawable/ic_launcher",
MainLauncher = true,
NoHistory = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
public class LoginActivity : FormsAppCompatActivity { }
The NoHistory = true part is the interesting one. This means it won't be included in the Android navigation stack so you can't go back to it using the back buttons.
1a) How to detect if device is iOS and Android phone? Since iOS has no
back Button, will onBackButtonPressed() apply to it?
No. OnBackButtonPressed does nothing on iOS. It will not be called.
2) User logout in the beginning: Login -> MainMenu: in MainMenu page,
user click Logout button will this cause any problem since the first
time login, Now what is the Mainpage?? when user click Logout button?
What happen when user login again? Which Navigation method I should
use to go back to Login Page?
You can swap out the MainPage as needed. This will also help you when it comes to the back button. On app startup you can check if the user is logged in already. If he is, you set the Current.MainPage to your main menu page. If not you set it to the login page. When the user has successfully logged in you set the Current.MainPage to the main menu page. Since you set the main page, you get a completely new navigation stack, so the back button will not make the app go back to the login page.
Related
Using Xamarin.Forms v4.5 and Prism v7.2
I have an APP with 3 pages (PageA, PageB and PageC).
From PageA, I can navigate to PageB and from PageB to PageC. From PageC, I can tap on the back button and go back to PageB.
My question is, when I navigate to PageB (either from PageA or PageC), is there a method that Prism provides to identify whether I end up to PageB by pressing the back button?
At this time Xamarin.Forms does not provide a way to hook into the underlying Navigation Service and intercept the full Navigation process in certain flows such as when someone clicks on a hardware back button. As a result we are limited in what we can do and are therefore not able to support IConfirmNavigation to allow you to prevent navigating away.
That having been said we are able to observe that a Page was popped and we are still able to invoke OnNavigatedFrom/OnNavigatedTo. You can easily determine what has occurred by getting the NavigationMode. To do this your code might look like this:
public void OnNavigatedTo(INavigationParameters parameters)
{
var mode = parameters.GetNavigationMode();
switch(mode)
{
case NavigationMode.New:
break;
case NavigationMode.Back:
break;
case NavigationMode.Forward:
break;
}
}
I am trying Model layout of Firebase in-app messaging. On model I have added one button. and for that button action, I have provided the firebase dynamic link. So when the user is clicking on button dynamic link is getting a trigger. and it's first opening the browser and after that, it's again coming to application. This interaction is not looking more natural. I want to open the new page on click of the action button. But on click of action button first browser is getting open. How to solve this issue?
You can implement FirebaseInAppMessagingClickListener in your mainActivity and initialize in onCreate() method using FirebaseInAppMessaging.getInstance().addClickListener(MainActivity.this);
for more details please click below link-
https://firebase.google.com/docs/in-app-messaging/modify-message-behavior?platform=android
Simplest way to achieve this would be to extend your MainActivity with FirebaseInAppMessagingClickListener if you know that the In-App Message will be triggered there & then create & Intent & open another activity.
Add the messageClicked function to your class:
#Override
public void messageClicked(InAppMessage inAppMessage, Action action) {
// Determine which URL the user clicked
String url = action.getActionUrl();
// Get general information about the campaign
CampaignMetadata metadata = inAppMessage.getCampaignMetadata();
// Trigger an Intent to another class
}
And register it (in your MainActivity) too:
FirebaseInAppMessaging.getInstance().addClickListener(this);
PS: In the case that you schedule your In-App Message to be triggered on a certain event, implement this code in the activity there.
I use the following code to start a freshhmvvm master-detail page from a list in a navigation page. This works fine, but the user can not go back to the list page on IOS. On android it is possible using the system back-button from each detailpage.
Does this mean I have to add a 'Back' button to each detailpage to make this work on IOS?
Or are there other ways to start a masterdetail in freshmvvm?
var masterDetailNav = new FreshMasterDetailNavigationContainer();
masterDetailNav.Init("Menu");
masterDetailNav.AddPage<FirstPageModel>("First", item);
masterDetailNav.AddPage<SecondPageModel>("Second", item.id);
await CoreMethods.PushNewNavigationServiceModal(masterDetailNav);
Lets say the first page in the app is the login page and then it takes me to do the main menu screen, is there a way to get rid of the back button in the main menu navigation bar, like get rid of the login page stack?
thank you
In Xamarin.Forms 1.3 and greater you can use
NavigationPage.SetHasBackButton(this, false);
In Xaml you can add:
<ContentPage ....NameSpaces etc....
NavigationPage.HasBackButton="False"
Title="MyPage">
</ContentPage>
You can avoid having the Back button if you replace the Navigation.PushAsync(page) to Navigation.PushModalAsync(page) in your login page's code. Post some code if this somehow doesn't apply
This has to do with how navigation works in the underlying OS (at least in iOS that's the case) - there is a Navigation Controller which serves for pages to transition between each other and have a trace of previous screen so the user can go back.
There are 2 ways to get rid of back button:
1) You can remove navigation bar from Xaml using Xamarin.Forms using below code
NavigationPage.SetHasNavigationBar (this, false);
Where this stands for current page / form instance.
2) Follow below mentioned steps
Navigate to login page when the app is loaded with Normal ContentPage instance of Login Page
Navigate to Main page from Login page using PushModalAsync and provide the main page instance as NavigationPage
And then from all the other pages, you can use PushAsync and it'll let you navigate to all the pages without any error.
Hope this helps!
By using CustomRenderer, you call this function in ViewWillAppear in your customized view controller
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
this.ParentViewController.NavigationItem.SetHidesBackButton (true, false);
//remember to use ParentViewController to reference to the NavigationViewController (if your contentPage is direct under a navigation controller. I don't know why but Xamarin must have a bug with SetHidesBackButton. If you call with this.NavigationItem.SetHidesBackButton(...), it should not work.
... other implements here ...
}
I created a button if i do click event the view must be dismissed and show the previous view. I used the below code:
partial void canceldata (NSObject sender)
{
Console.WriteLine("canceldata");
//NavigationController.PopToViewController(settrls,true);
this.NavigationController.PopViewControllerAnimated(true);
}
But this code is not working. Anybody suggest me how to dismiss the current view by clicking a button in iOS mono touch example.
It depends on how you open that controller. If it is pushed to the stack, then PopViewControllerAnimated() should be working. If it is presented as a modal dialog for example by
NavigationController.PresentViewController(new UINavigationController(new MyDialog()), true, null);
then you should call
NavigationController.DismissViewController(true, null);
instead.