How to get access to getSupportFragmentManager or SupportFragmentManager in fragment

谁都会走 提交于 2020-06-12 02:16:49

问题


The name 'getSupportFragmentManager' does not exist in the current context

My Code:

using Android.Views;
using Android.OS;
using Android.Support.V4.App;
using com.refractored;
using Android.Support.V4.View;

namespace XamarinStore
{
    public class HomeFragment : Android.App.Fragment
    {
        BadgeDrawable basketBadge;
        int badgeCount;

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            RetainInstance = true;
            SetHasOptionsMenu(true);



            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var HomePage = inflater.Inflate(Resource.Layout.HomeLayout, container, false);

            var pager = HomePage.FindViewById<ViewPager>(Resource.Id.pager);
            pager.Adapter = new MyPagerAdapter(SupportFragmentManager);


            var tabs = HomePage.FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
            tabs.SetViewPager(pager);

            return HomePage;
        }
    }

    public class MyPagerAdapter : FragmentPagerAdapter{
        private  string[] Titles = {"Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
            "Top New Free", "Trending"};

        public MyPagerAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
        {
        }

        public override Java.Lang.ICharSequence GetPageTitleFormatted (int position)
        {
            return new Java.Lang.String (Titles [position]);
        }
        #region implemented abstract members of PagerAdapter
        public override int Count {
            get {
                return Titles.Length;
            }
        }
        #endregion
        #region implemented abstract members of FragmentPagerAdapter
        public override Android.Support.V4.App.Fragment GetItem (int position)
        {
            return SuperAwesomeCardFragment.NewInstance (position);
        }
        #endregion
    }
}

How to get access to getSupportFragmentManager in fragment ..

I'am using xamarin cross platform development tool.. with the help of sample project from this source https://xamarin.com/c-sharp-shirt

In the demo project if BackStackEntryCount count equals to zero it switch screen to another fragment.. so i replaced code with new homefragment i decided to add tabs in the that.. so i tried to use this component "Material Pager Sliding Tab Strip" .. while using that component it stops with this error. the name 'getSupportFragmentManager' does not exist in the current context

http://components.xamarin.com/view/PagerSlidingTabStrip


回答1:


First i inherit HomeFragment to Android.Support.V4.App.Fragment

Then i used ChildFragmentManager instead of SupportFragmentManager

pager.Adapter = new MyPagerAdapter(ChildFragmentManager);



回答2:


You can also do:

using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;


public class YourFragment : Fragment
{
//...

//To access to FragmentManager use base.FragmentManager
//Example:
base.FragmentManager.BeginTransaction()
    .Replace(Resource.Id.content_frame, fragment)
        .Commit();

//...
}

The first two lines (using...) are very useful if you work only with Fragment and FragmentManager from Support and you have clear about not mixing them with normal Fragment and FragmentManager.




回答3:


In Xamarin Android you should be able to access this using FragmentManager and you need to inherit from Android.Support.V4.App.Fragment like so:

 public class HomeFragment :  Android.Support.V4.App.Fragment
 {
    BadgeDrawable basketBadge;
    int badgeCount;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        RetainInstance = true;
        SetHasOptionsMenu(true);

        var count =  FragmentManager.BackStackEntryCount;

        // Create your fragment here
    }

getActivity 's equivalent in Xamarin is Activity (when used in a class that inherits from Android.Support.V4.App.Fragment for example)




回答4:


I am not familiar with Xamarin but in native android development you cannot access FragmentManager from a Fragment using getFragmentManager or getSupportFragmentManager methods. From API 17 you can call getChildFragmentManager method to get a FragmentManager instance that allows you to manage fragments from another fragment.

getChildFragmentManager().beginTransaction().add(...).commit();
getChildFragmentManager().executePendingTransactions();



回答5:


public class HomeFragment : FragmentActivity

Extend your class using FragmentActivity will solve your problem



来源:https://stackoverflow.com/questions/29531643/how-to-get-access-to-getsupportfragmentmanager-or-supportfragmentmanager-in-frag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!