简体   繁体   中英

How do I go back to the top of the code?

I have been learning C# and I am creating an app.

I have tried goto statements however the labels are "out of range" I want it to send me back to the original layout which I found out I need to go back to the top of the code.

            namespace TheAppOfJack
            {
            [Activity(Label = "The App Of Jack", MainLauncher = true, Icon = "@drawable/icon")]
            public class MainActivity : Activity
            {

            int count = 1;

            protected override void OnCreate(Bundle bundle)
            {

            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            Button button1 = FindViewById<Button>(Resource.Id.MyButton);
            button1.Click += delegate { button1.Text = string.Format("You have clicked this random button {0} times ( ͡° ͜ʖ ͡°)", count++); };

            Button button2 = FindViewById<Button>(Resource.Id.button1);
            button2.Click += delegate
            {
            SetContentView(Resource.Layout.Gallery);
            Button button3 = FindViewById<Button>(Resource.Id.back);
            button3.Click += delegate { SetContentView(Resource.Layout.Main); };

            Button button4 = FindViewById<Button>(Resource.Id.next1);
            button4.Click += delegate
            {
            SetContentView(Resource.Layout.Gallery2);
            button3.Click += delegate { SetContentView(Resource.Layout.Main); };
            Button button5 = FindViewById<Button>(Resource.Id.next2);
            button5.Click += delegate
            {

            SetContentView(Resource.Layout.Gallery3);
            Button button6 = FindViewById<Button>(Resource.Id.home);
            button6.Click += delegate {

            //this is where i want the code to send me back to the top };
            };
            };
            };
            }
            }
            }

The way you are trying is not how Android works. You cannot navigate between 'layouts', instead you navigate between activities which have a layout associated with them. This navigation is done using the StartActivity() method.

The MainActivity is the first activity that will be shown when your app launches. It's associated layout (which is named as Main here) should have only one button that will take you to the next screen. Suppose its ID is btnForActivity2 . Your OnCreate for MainActivity will be like this:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Main);

    Button btn = FindViewById<Button>(Resource.Id.btnForActivity2);
    btn.Click += delegate
    {
        StartActivity (typeof(MyActivity2));
    };
}

In the MyActivity2 class, let's say you want to have two buttons to take you to two new activities. Assume one activity is called MyActivity3 and the other is called MyActivity4 . In this case, the OnCreate method of your MyActivity2 will be something like this:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    SetContentView(Resource.Layout.MyActivity2Layout);

    Button btn = FindViewById<Button>(Resource.Id.btnForActivity3);
    btn.Click += delegate
    {
        StartActivity (typeof(MyActivity3));
    };

    btn = FindViewById<Button>(Resource.Id.btnForActivity4);
    btn.Click += delegate
    {
        StartActivity (typeof(MyActivity4));
    };
}

This is how basic navigation works in Android.

By the way, you may have noticed that I have not talked about a Back button. It's because I have never explicitly implemented it as, unlike iOS devices, Android devices have a button specifically for that purpose. However, if you must have a button that takes you back to the previous activity then simply calling this.Finish(); should suffice. Once you call Finish() for an activity, it is removed from the top of the activities stack and the activity below becomes visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM