CompanyDroneFromSector7G
CompanyDroneFromSector7G

Reputation: 4517

Click events for multiple pages

How do I assign click events to buttons in an app with multiple pages?

I can do it fine with a single page app, as follows:

namespace Welf
{
    [Activity(Label = "Welf", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Assign click event to direct to next page (page1)
            Button cmdFirst = FindViewById<Button>(Resource.Id.cmdFirst);
            cmdFirst.Click += delegate
            {
                SetContentView(Resource.Layout.page1);
            };
        }
    }
}

But at what point to I assign click events for buttons on other pages?

If I do the following it doesn't work, I'm guessing because the pages aren't instantiated yet so FindByView returns a null:

    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Assign click event to direct to next page (page1)
            Button cmdFirst = FindViewById<Button>(Resource.Id.cmdFirst);
            cmdFirst.Click += delegate
            {
                SetContentView(Resource.Layout.page1);
            };

            // This assignment doesn't work
            Button cmdSecond = FindViewById<Button>(Resource.Id.cmdSecond);
            cmdSecond.Click += delegate
            {
                SetContentView(Resource.Layout.page3);
            }
        }
    }

...and the following does work, but then I get into trouble with repeat visits to the same page because I'm adding the event a second time:

    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Assign click event to direct to next page (page1)
            Button cmdFirst = FindViewById<Button>(Resource.Id.cmdFirst);
            cmdFirst.Click += delegate
            {
                SetContentView(Resource.Layout.page1);

                // This click works the first time only, for obvious reasons
                Button cmdSecond = FindViewById<Button>(Resource.Id.cmdSecond);
                cmdSecond.Click += delegate
                {
                    SetContentView(Resource.Layout.page3);
                }
            };
        }
    }

So how should I do it?

Upvotes: 0

Views: 106

Answers (1)

Launch9
Launch9

Reputation: 24

You should only call SetContentView() once in any activity. What you need to do here is make a new activity. 1: Right click on your project and click new item. 2: Create a new activity and a new Android layout. 3: Then in your new Activity.cs call SetContentView for your new layout you just created. 4: Then use this code to call your new activity: Intent intent = new Intent(this,typeof(mynewactivityname)); StartActivity(intent);

Upvotes: 1

Related Questions