Navigation Using StartActivity & Hardware Back Button

Nathan Sokalski 4,106 Reputation points
2021-08-11T02:30:08.15+00:00

I have an app that has 2 pages/Activity(s). To navigate between the two, I use one of the following:

this.StartActivity(new Intent(this, typeof(SettingsActivity)));

this.StartActivity(new Intent(this, typeof(MainActivity)));

This works fine when I call them from codebehind (such as a Button Click), which is where I usually want to call them, but when the user clicks the hardware back button, I want it to do one of the following:

  1. If the user is on the MainActivity page, I want it to do the default action of minimizing (or whatever the correct term is in Android) the app
  2. If the user is on the SettingsActivity page, I want to either go to the MainActivity page or minimize the app based on a condition I specify in code

In order to do this, I obviously need to override OnBackPressed (I do not currently override OnBackPressed for either Activity). I am not extremely familiar with running multiple Activity(s) at the same time, which I am guessing is related since I use StartActivity. Do I need to somehow stop the current Activity after navigating to the new one? Should I be using a different style of navigation (I want to keep my navigation simple since I have only 2 pages)? Any help would be appreciated.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,357 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,961 Reputation points
    2021-08-12T02:13:44.51+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To achieve this navigation style, try setting the LaunchMode to SingleTask. In SingleTask mode, there can be only one instance of an Activity in the stack. If there are already other activity instances in the stack, starting this Activity in other activities will directly kick other activity instances out of the stack.

    If you set the MainActivity as the main launcher, clicking the back button in the SettingActivity will always go back to MainActivity. And clicking the back button in MainActivity will close the application.

       [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTask)]  
       public class MainActivity : AppCompatActivity  
       {  
           ...  
           private void Button_Click(object sender, EventArgs e)  
           {  
               StartActivity(new Intent(this, typeof(SettingActivity)));  
           }  
       }  
         
       [Activity(Label = "SettingActivity", LaunchMode = LaunchMode.SingleTask)]  
       public class SettingActivity : AppCompatActivity  
       {  
           ...  
           private void Button_Click(object sender, EventArgs e)  
           {  
               StartActivity(new Intent(this, typeof(MainActivity)));  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.