Nested fragments are not working on back click in xamarin android

Khair Muhammad 21 Reputation points
2021-03-11T08:33:19.39+00:00

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();
Developer technologies .NET Xamarin
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-03-11T11:40:45.497+00:00

    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 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.