user3034944
user3034944

Reputation: 1751

Switch fragments without replacing Android

I have three fragments and a Navigation drawer in my app viz, HomeFragment,DetailFragment and CountryFragment. Now when Iam in HomeFragment(Default fragment), I check a condition, if it returns false, I want to redirect to the CountryFragment without replacing the fragment.

Initially, in the host activity Iam doing something like this:-

HomeActivity.

private Android.Support.V4.App.Fragment mCurrentFragment = new SupportFragment();
        private Stack<Android.Support.V4.App.Fragment> mStackFragments;
 protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.homelayout);
      Android.Support.V4.App.FragmentTransaction tx = SupportFragmentManager.BeginTransaction();

                tx.Add(Resource.Id.main, homeFragment);
                tx.Add(Resource.Id.main, countryFragment);
                tx.Hide(countryFragment);
                tx.Add(Resource.Id.main, detailFragment);
                tx.Hide(detailFragment);

                CurrentFragment = homeFragment;
                tx.Commit();
}

void MenuListView_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
        {
            Android.Support.V4.App.Fragment fragment = null;

            switch (e.Id)
            {
                case 0:
                    ShowFragment(homeFragment);
                    break;
                case 1:
                    ShowFragment(countryFragment);
                    break;
                case 2:
                    ShowFragment(detailFragment);
                    break;


            }

         }




  public  void ShowFragment(SupportFragment fragment)
    {

        if (fragment.IsVisible)
        {
            return;
        }

        var trans = SupportFragmentManager.BeginTransaction();


        fragment.View.BringToFront();
        mCurrentFragment.View.BringToFront();

        trans.Hide(mCurrentFragment);
        trans.Show(fragment);

        trans.AddToBackStack(null);
        mStackFragments.Push(mCurrentFragment);
        trans.Commit();

        mCurrentFragment = fragment;

    }

Next in HomeFragment Iam doing something like :-

public void BtnCompleteSubmit_Click(object sender, EventArgs e)
        {
            dlgAlert.Dismiss();
            CountryFragment countryFrag = new CountryFragment ();
            // Create new fragment and transaction
            var myActivity = (HomeActivity)this.Activity;

            myActivity.ShowFragment(countryFrag );


        }

This does go to the ShowFragment() of the HomeActivity, but the view for the fragment is null even thought the View was created initially while the NavigationDrawer was created. How do I solve this? any help is appreciated.

Upvotes: 0

Views: 112

Answers (1)

zIronManBox
zIronManBox

Reputation: 5067

What you can do is create all fragment with a String Tag name associated. Then add it your fragment transcation using fragment manager. After if you want to shift between the fragments, then write an if else statement and find the fragment by tag method.

if (mFragManager.findFragmentByTag("FRAG_HOME") != null)
            showFragment(mFragManager.findFragmentByTag("FRAG_HOME"));
        else
            showFragment(new homeFragment(), "FRAG_HOME");

In you show Fragment method:

public  void ShowFragment(Fragment fragment, String tag)
{

        mFragTransaction= mFragManager.BeginTransaction();
if (fragment.isAdded())
                mFragTransaction.show(fragment);
            else
                mFragTransaction.add(ResId, fragment, tag).setBreadCrumbShortTitle(tag);

                mFragTransaction.addToBackStack(tag);

            mFragTransaction.commit();

}

Upvotes: 1

Related Questions