使用片段Xamarin Android的设备向后压



我有一个活动,从中调用了三个片段。现在,我想使用后压法回到xamarin.android中的上一个片段或屏幕?如何实现?

这将完成工作:

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using SupportTolbar = Android.Support.V7.Widget.Toolbar;
    using Android.Support.V7.App;
    using Android.Support.V4.Widget;
    using Android.Graphics;
    using SupportFragment = Android.Support.V4.App.Fragment;
    using Android.Content.PM;
    using Android.Graphics.Drawables;
    using System.Threading.Tasks;
    using System.Net.Http;
    using Newtonsoft.Json;
    namespace App
    {
        [Activity(Label = "", Theme = "@style/MyTheme", ScreenOrientation = ScreenOrientation.Portrait)]
        public class HomeActivity : AppCompatActivity
        {
            //Views that switch the fragments 
            public ImageView _viewTab1;
            public ImageView _viewTab2;
            public ImageView _viewTab3;
            //fragmentContainer 
             private FrameLayout _frameLayoutFragmentContainer;
            //fragments 
            public SupportFragment _currentFragment;
            public SupportFragment _customFragment1;
            public SupportFragment _customFragment2;
            public SupportFragment _customFragment3;
            private Stack<SupportFragment> _StackFragment;

            protected async override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.Main);
                 _frameLayoutFragmentContainer = FindViewById<FrameLayout>(Resource.Id.frameContainer);
                //initialize views that switch fragments
                _viewTab1 = FindViewById<ImageView>(Resource.Id.viewTab1);
                _viewTab2 = FindViewById<ImageView>(Resource.Id.viewTab2);
                _viewTab3 = FindViewById<ImageView>(Resource.Id.viewTab3);
                //catch events on button click what ever is the thing that switches the fragments
                _viewTab1.SetOnClickListener(new TabOnClickListener(this));
                _viewTab2.SetOnClickListener(new TabOnClickListener(this));
                _viewTab3.SetOnClickListener(new TabOnClickListener(this));
               //create fragments
                _customFragment1 = new Fragment1();
                _customFragment3 = new Fragment2();
                _customFragment2 = new Fragment3();
                _currentFragment = _customFragment1;
                _StackFragment = new Stack<SupportFragment>();
                var trans = SupportFragmentManager.BeginTransaction();
                trans.Add(Resource.Id.frameContainer, _customFragment1, "First Fragment");
                trans.Commit();
            }
            public override void OnBackPressed()
            {
                base.OnBackPressed();
            }
            public void ReplaceFragment(SupportFragment fragment) {
                if (fragment.IsVisible)
                    return;
                var trans = SupportFragmentManager.BeginTransaction();
                trans.Replace(Resource.Id.frameContainer, fragment);
                trans.AddToBackStack(null);
                trans.Commit();
                _currentFragment = fragment;
        }
        }
        public class TabOnClickListener : Java.Lang.Object, View.IOnClickListener
        {
            private HomeActivity homeActivity;
            public TabOnClickListener(HomeActivity homeActivity)
            {
                this.homeActivity = homeActivity;
            }
            public void OnClick(View v)
            {
                switch (v.Id)
                {
                    case Resource.Id.viewTab1:
                        homeActivity.ReplaceFragment(homeActivity._customFragment1);
                        break;
                case Resource.Id.viewTab2:
                        homeActivity.ReplaceFragment(homeActivity._customFragment2);
                        break;
                case Resource.Id.viewTab3:
                        homeActivity.ReplaceFragment(homeActivity._customFragment3);
                        break;
                }
            }
        }
    }

我做了静态委托:

static class FragmentOnBackRouter
    {
        public static Action RegisteredAction { get; set; }
    }

我在这里使用的内容:

public override void OnBackPressed()
        {
            if (FragmentOnBackRouter.RegisteredAction != null)
                FragmentOnBackRouter.RegisteredAction.Invoke();
            else
                base.OnBackPressed();
        }

然后,您只需分配委托 - 通常将其分配在片段的Onstart方法中,然后以OnStop方法传递NULL。简单的。对于函数,只需使用generics(键入函数&lt; t>)。

我不会使用太多活动(每个应用程序2-3),因此很容易,如果我必须更复杂,您可以随时创建一些抽象活动,继承和覆盖在那里。

相关内容

  • 没有找到相关文章

最新更新