question

KhairMuhammad-3541 avatar image
0 Votes"
KhairMuhammad-3541 asked RobCaplan edited

Nested fragments are not working on back click in xamarin android

I have created one activity and 2 fragments. Fragment B is inside Fragment A. When I click on back button on my phone it doesn't go to Fragment A from Fragment B. But it crashes the app. I am maintaining a Stack.

 public void ShowFragment(SupportFragment fragment)
         {
             if (fragment.IsVisible)
             {
                 return;
             }
             Android.Support.V4.App.FragmentTransaction trans = SupportFragmentManager.BeginTransaction();
             fragment.View.BringToFront(); 
             trans.Hide(mCurrentFragment);
             Console.WriteLine("Hiding fragemnt current " + mCurrentFragment.Tag+" fragment "+fragment.Tag);
             trans.Show(fragment);
            
             if (mStackFragments.Count != 0)
             {
                 SupportFragment sf = mStackFragments.Pop();
                 if (fragment != sf)
                 {
                     mStackFragments.Push(sf);
                     mStackFragments.Push(fragment);
                 }
                 else
                 {
                     mStackFragments.Push(sf);
                 }
             }
             else
             {
                 mStackFragments.Push(fragment);
             }
             mCurrentFragment = fragment;
             trans.Commit();
             LoadFragmentTitleTxtView();
             LoadActionBarSpinner();
         }

above method is used to show the fragment/main screen, whereas the following code is for OnBackPressed.

 public override void OnBackPressed()
         {
             if (mDrawerLayout.IsDrawerOpen((int)GravityFlags.Left))
             {
                 mDrawerLayout.CloseDrawer((int)GravityFlags.Left);
             }
             else
             {
                 if (mStackFragments.Count > 1)
                 {
                     //mStackFragments.Pop();
                     Console.WriteLine("Popping 1st " + mStackFragments.Pop().Tag+" Count "+mStackFragments.Count);
                     SupportFragment frag = mStackFragments.Pop();
                     Console.WriteLine("Poppoing 2nd " + frag.Tag+" Count "+mStackFragments.Count);
                     if (frag == chaptersFragment)
                     {
                         if (chapterfiltertext != "")
                         {
                             item.ExpandActionView();
                             sssearchview.SetQuery(chapterfiltertext, false);
                             mainview.Animate().Alpha(1f).SetListener(null);
                         }
                     }
                     Console.WriteLine("In back method.. "+frag.Tag);
                     ShowFragment(frag);
                 }
                 else
                 {
                     Finish();
                 }
             }
         }

its working fine for the fragments which doesn't have any nested fragments within them.

This is the code for the nested fragment B to load it on main screen from fragment A.

 FragmentTransaction tx = context.SupportFragmentManager.BeginTransaction();
             FragmentB fragmentB = new FragmentB();
             Bundle args = new Bundle();
             args.PutString("letter", tmp1);
             fragmentB.Arguments = args;
             tx.Replace(Resource.Id.main, fragmentB, "innerDictFrag");
             tx.AddToBackStack(null);
             context.mStackFragments.Push(fragmentB);
             tx.Commit();
dotnet-csharpdotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
1 Vote"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

If you want to transfer fragment1 to fragment2, please use the following code, do not change code order. For example, current is fragment1, then I navigate to fragment2

public class Fragment1 : Fragment
    {

      
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            

            View view=   inflater.Inflate(Resource.Layout.layout1, container, false);

            Button button1 = view.FindViewById<Button>(Resource.Id.button1);
            button1.Click += Button1_Click;
            return view;
          
        }

        private void Button1_Click(object sender, EventArgs e)
        {
         
            FragmentTransaction transaction = this.FragmentManager.BeginTransaction();
            Fragment2 fragment =   new Fragment2();
            transaction.AddToBackStack(null);
            transaction.Replace(Resource.Id.FramePage, fragment);
            transaction.Commit();
        }
    }


In the fragment2, you can click the back button to back the fragment1, or click button execute this.FragmentManager.PopBackStack();

public class Fragment2 : Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
           View view= inflater.Inflate(Resource.Layout.layout2, container, false);
            Button popBtn = view.FindViewById<Button>(Resource.Id.popBtn);
            popBtn.Click += PopBtn_Click;
            
            return view;

        
        }

        private void PopBtn_Click(object sender, EventArgs e)
        {
           

            this.FragmentManager.PopBackStack();
        }
    }


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KhairMuhammad-3541 Did you try to use above code? It could make the fragement in the navigation stack, you can back to previous fragment by this.FragmentManager.PopBackStack();

0 Votes 0 ·