Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep only first added Fragment in back stack (fragment overlapping)?

Tags:

android

Scenario what i'm trying to achieve:

  1. Loading activity with two frame containers (for list of items and for details).
  2. At the app launch time add listFragment in listFrame and some initial infoFragment in detailsFrame containers.
  3. Navigating through list items without adding each detail transaction to back stack (want to keep only infoFragment in stack).
  4. As soon as user hit back button (navigate back) he falls back to intial infoFragment what was added in launch time.
  5. If sequential back navigation fallows then apps exit.

My code:

        protected override void OnCreate(Bundle savedInstanceState)
        {
...
            var listFrag = new ListFragment();
            var infoFrag = new InfoFragment();
            var trans = FragmentManager.BeginTransaction();
            trans.Add(Resource.Id.listFrame, listFrag);
            trans.Add(Resource.Id.detailsFrame, infoFrag);
            trans.Commit();
...
        }

        public void OnItemSelected(int id)
        {
            var detailsFrag = DetailFragment.NewInstance(id);
            var trans = FragmentManager.BeginTransaction();
            trans.Replace(Resource.Id.detailsFrame, detailsFrag);
            if (FragmentManager.BackStackEntryCount == 0)
                {
                    trans.AddToBackStack(null);
                }
            trans.Commit();
        }

My problem:

After back button has been hit, infoFrag is overlapped with previous detailFrag! Why?

like image 626
Arvis Avatar asked Jan 10 '13 23:01

Arvis


2 Answers

You can do this:

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
   getSupportFragmentManager().popBackStack(getSupportFragmentManager().getBackStackEntryAt(0).getId(), getSupportFragmentManager().POP_BACK_STACK_INCLUSIVE);
} else {
   super.onBackPressed();}

In your activity, so you to keep first fragment.

You shouldn't have, in your first fragment, the addToBackStack. But, in the rest, yes.

like image 113
Juan Labrador Avatar answered Oct 12 '22 23:10

Juan Labrador


Very nice explanation by Budius. I read his advice and implemented similar navigation, which I would like to share with others.

Instead of replacing fragments like this:

Transaction.remove(detail1).add(detail2)
Transaction.remove(detail2).add(detail3)
Transaction.remove(detail3).add(detail4)

I added a fragment container layout in the activity layout file. It can be either LinearLayout, RelativeLayot or FrameLayout etc.. So in the activity on create I had this:

transaction.replace(R.id.HomeInputFragment, mainHomeFragment).commit();

mainHomeFragment is the fragment I want to get back to when pressing the back button, like infoFrag. Then, before EVERY NEXT transaction I put:

fragmentManager.popBackStackImmediate();
transaction.replace(R.id.HomeInputFragment, frag2).addToBackStack(null).commit();

or

fragmentManager.popBackStackImmediate();
transaction.replace(R.id.HomeInputFragment, frag3).addToBackStack(null).commit();

That way you don't have to keep track of which fragment is currenty showing.

like image 27
mdzeko Avatar answered Oct 13 '22 00:10

mdzeko