1

I have used VS 2017 to create an Android App (Xamarin) Navigation Drawer App. I have searched the internet for an example of how to add a new activity to the app that uses the already created Navigation Drawer but have been unsuccessful. Any idea's on how to add an activity?

Thanks Paul.

public bool OnNavigationItemSelected(IMenuItem item)
    {
        int id = item.ItemId;

        if (id == Resource.Id.nav_camera)
        {
            // Run a new activity here!
        }
        else if (id == Resource.Id.nav_gallery)
        {

        }
        else if (id == Resource.Id.nav_slideshow)
        {

        }
        else if (id == Resource.Id.nav_manage)
        {

        }
        else if (id == Resource.Id.nav_share)
        {

        }
        else if (id == Resource.Id.nav_send)
        {

        }

        DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        drawer.CloseDrawer(GravityCompat.Start);
        return true;
    }
  • Do you want to start a Activity if you click the button? – Robbit Jun 18 '18 at 03:14
  • Yes, I would like to start an activity on the navigation drawer item click. I am new to Xamarin Android so I guess an activity is another page? – Paul Wainwright Jun 18 '18 at 06:39
  • Have you tested my answer? If you want to go to another page, you need to create a new Activity in your project. – Robbit Jun 18 '18 at 06:44
  • Hello, still can't solve your problem? Maybe [this](https://diptimayapatra.wordpress.com/2013/07/03/xamarin-opening-another-activity-in-android-application/) can help you. – Robbit Jun 18 '18 at 06:58
  • Hello, I have posted a example on [github](https://github.com/xiaolvzi/FragmentWithDrawerlayout) in c#. – Robbit Jun 18 '18 at 07:33

1 Answers1

1

From here, you can see this:

If your app switches out content based on which navigation menu item the user selects, you should consider using fragments in the main content area. Swapping fragments when you navigate from the navigation drawer allows for a seamless drawer animation, because the same base layout stays in place.

Official suggest us use fragment in the main content area.

If you want to start a new Activity, you need create a Activity and create a layout for it, like Activity1 :

[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here

        SetContentView(Resource.Layout.layout1);
    }
}

layout1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="new Activity" />
</LinearLayout>

And then start it:

if (id == Resource.Id.nav_camera)
{
    Intent intent = new Intent(this, typeof(Activity1));
    StartActivity(intent);
}

This will open a new Activity, and in the new Activity, there is no DrawerLayout.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • I previously tried code similar to yours above and the navigation drawer did indeed disappear. Do you have a fragment example in C#? – Paul Wainwright Jun 18 '18 at 07:03
  • Yes, if you start a new Activity, navigation drawer will disappear, because, the `DrawerLayout` exists in your first Activity, not your new Activity. – Robbit Jun 18 '18 at 07:06
  • About Fragment example, you can read [this](https://stackoverflow.com/a/44237794/8632294), and I think you should open another thread to ask how to switch fragment with `DrawerLayout`. BTW, if my answer has helped you, please accept it as answer, thanks. – Robbit Jun 18 '18 at 07:09
  • You can read [this](https://stackoverflow.com/questions/25822656/what-are-the-differences-between-activity-and-fragment) to understand the difference between activity and fragment. – Robbit Jun 18 '18 at 07:14
  • Thanks Joe Lv, your GitHub sample was exactly what I was looking for, I think the VS template should include this code. Paul. – Paul Wainwright Jun 18 '18 at 09:01
  • Glad it can help you, would you mind give my answer a vote up? – Robbit Jun 18 '18 at 09:03