1
votes

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

1

1 Answers

1
votes

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.