-1

I'm trying to switch layout from full view to double pane view like following:

.......     .......
.     .     .     .
.     . --> .......
.     .     .     .
.......     ....... 

Now the issue is I'm going from 1 container with fragment to 2 containers with a fragment in each. In order to do this I'm removing the old fragment and adding the new ones. BUT when I remove the fragment in the large one an OLD fragment appears instead. I've set tag on the old fragment and I search after it with getFragmentManager().findFragmentByTag() but it simply returns null.

For now my hack is use largeView.setVisibility(View.gone); is there a better way to handle this?

Edit:

It seems like the bug isn't reproduced any longer. That means replace does what it should have done all along. But WHY did it glitch? Eclipse? Android?

Edit2

This is how I replace fragments:

Fragment fragment = new SimpleScannerFragment();

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main_container, fragment, SCAN_FRAGMENT_TAG);
ft.commit();

Before switching to double pane layout I pop back stack like this:

getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

I remove fragments using this:

removeFragment = getFragmentManager().findFragmentByTag(SCAN_FRAGMENT_TAG);
if(removeFragment != null) {
    getFragmentManager().beginTransaction().remove(removeFragment).commit();
}

I'm switching views visiblity when I go from single to double pane:

    // hide main layout as well, THIS IS MY HACKS just in case it does this again...
    FrameLayout frame = (FrameLayout) this.findViewById(R.id.main_container);
    frame.setVisibility(View.GONE);

    // add new layout
    FragmentTransaction ft = getFragmentManager().beginTransaction();

    CustomMapFragment fragment = CustomMapFragment.newInstance();
    ScoreFragment scoreFragment = new ScoreFragment();

    ft.replace(R.id.main_narrow_bot_container, fragment);
    ft.replace(R.id.main_narrow_top_container, scoreFragment);
    ft.commit();
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • what about just keeping an instance of the created fragment then removing it by using the instance you are holding onto when you created it – tyczj Oct 09 '14 at 14:12
  • but I'm replacing my fragments when I switch them. I should be able to find them by tag if they exist anyway. I'll give it a go but still doesn't seem right. – Warpzit Oct 09 '14 at 15:29
  • did you try using findFragmentById? – tyczj Oct 09 '14 at 15:31
  • @tyczj they don't have an ID. I don't create them in xml. Technially they have one, but it doesn't make sense to use. – Warpzit Oct 09 '14 at 15:35
  • 1
    please post your code here – Jayesh Oct 21 '14 at 12:52
  • 1
    What do you mean "an *old* fragment" ? There may be something with the way you change your fragment from "old" to "new" in the first place (before trying to change from the new one to two other ones as in your question), so please post the code of _all_ your fragment changes and where they appear. – desseim Oct 21 '14 at 12:56

4 Answers4

1

How about trying to clear the entire back stack?

private void clearBackStack(){
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentManager.popBackStackImmediate();
    }
Andre Perkins
  • 7,640
  • 6
  • 25
  • 39
  • I actually used replace and cleared back stack. So it didn't make sense that it still showed up, also I can't reproduce any longer, so might just have been a glitch... – Warpzit Oct 25 '14 at 11:28
  • thats weird...but I am glad you were able to get past that issue :) – Andre Perkins Oct 26 '14 at 14:25
0

you just need to get Instance of FragmentManager.

then use this magic line.

 fm.replace(R.id.fragment_main, frag).commit();
Danial Hussain
  • 2,488
  • 18
  • 38
0

replace method is bugged (at least of API19). It's better to use combination of add and delete instead. But im not sure it's fixed in API21.

localhost
  • 5,568
  • 1
  • 33
  • 53
0

With this you can remove of the backStack all the fragments associate with FragmentManager:

FragmentManager fm = getActivity().getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {    
    fm.popBackStack();
}

It is similar if you want remove all the fragments associate with childFragmentManager or SupportFragmentManager.

Hope it helps you!!