• Navigate Android Activities using Explicit Intent in Android App using Xamarin, C#, and Visual Studio

    by  • December 27, 2016 • Android with C# and .NET, Xamarin Mobile App Development • 0 Comments

    As we’re working on MyBookStore Android App, we have reached to a point where we can navigate from one Activity to another Activity. We need to specify a particular Activity to which we need to navigate to and this is known as Explicit Intent. This means, we need to explicitly define which activity we need to go to. There is another type of intent called Implicit Intent which I’ll discuss in a separate post in future in this series.

    Let’s get started with this post. Earlier we had only one activity named MainActivity, while in order to be able to navigate to a new activity, we need to add more activities. For our purpose, we’ll add three new activities namely AboutAppActivity, ViewBookActivity, and ViewCartActivity. We’re keeping the app simple, but in a complex application, you can add as many activities as you like.

    To add a new activity, you need to right click MyBookStore project, click Add, and select Class from the menu.

    add-activity-class-1

     

    This will open up Add New Item window, from where you can select C# -> Android -> Activity and name your activity as required. It’s better to use Activity at the end of the selected name like ViewCartActivity.cs, instead of just ViewCart.cs.

    add-android-activity-class-2

    You can also change the Label of your activity which will be shown when the activity is displayed on screen in the Android.

    change-activity-label

    Now, it’s time to find controls from the activity view in the onCreate method. In our case, we need to find the buttons, so we could click those and then navigate to the required activities. We’ll first declare private variables and then find the views using FindViewById method.

    create-private-button-controls-and-find-button-resources

    Once we have found the controls from the view, we need to attach the click events to these buttons.

    attach-click-events-to-buttons

    Now we need to add the code to create a new explicit intent and then start the new activity.

    add-code-to-button-click-to-start-new-activity

    When you click on View Books button the following new activity will be displayed on screen.

    2-navigated-activity

    If we look at the code for the MainActivity from where we’ll navigate to other activities, this is how it should look like.

    Enjoy this post and we’ll talk in the next post soon. 🙂




    Namecheap.com
    Avatar of admin

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply