0

I have a navigation drawer with only one activity and several fragments. My main fragment is really heavy to load so I try to keep it in memory because if I don't do it the drawer will be laggy when I use it.

To do so I have a layout like this :

<RelativeLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</RelativeLayout>

<fragment
    android:id="@+id/main_fragment"
    android:name=".MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

They they the same place but in the code I replace the fragment layout with lighter fragment and show/hide the main fragment. This is a showFragment method to is called when I click on the item in the drawer.

case 0: //Light Fragment
    fragment = getSupportFragmentManager().findFragmentByTag("light");
    if (fragment == null) {
        fragment = ProfileFragment.newInstance();
    }
    getSupportFragmentManager().beginTransaction().hide(getSupportFragmentManager().findFragmentById(R.id.main_fragment)).commit();
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment, "light").addToBackStack(null).commit();
break;
case 1: //Heavy Frag
    if (getActiveFragment() != null) {
        getSupportFragmentManager().beginTransaction().remove(getActiveFragment()).commit();
    }
    getSupportFragmentManager().beginTransaction().show(getSupportFragmentManager().findFragmentById(R.id.main_fragment)).addToBackStack(null).commit();
break;

Which works fine except when I push the back button. I try many implementation of the onBackPressed but I always end up with either the main fragment showing when it should be hidden (So 2 fragment in top of each other) and the main fragment simply doesn't show.

Bonus question: Is that the correct way of doing thing ? I mean to avoid the drawer to lag or should i completely change my implementation?

Thanks.

Neodigi
  • 193
  • 4
  • 19
  • Remove the line where you hide the main fragment. Use FragmentActivity, the back button should now remove fragments by the order they were created. –  Apr 16 '15 at 08:53
  • I cannot use FragmentActivity and the problem would be the same because if I don't hide the mainFragment (just replace it) when I back to it i'll get a laggy transition. – Neodigi Apr 16 '15 at 09:49

1 Answers1

0

Well, this should be more of a comment but since I cannot comment, I have to answer.

The reason could be that the fragments are not going into the backstack properly or in the order as they should.

I once created a project wherein I used Navigation Drawer with fragments. If I understand you correctly, what you want is to go to the last open fragment.

Try using the following code: Put it in onBackPressed()

    @Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
    this.finish();
} else {
    getFragmentManager().popBackStack();
}
}

Essentially, you are popping the last fragment that was added to the backstack.

kerry
  • 2,362
  • 20
  • 33
  • This is what i'm doing but it doesn't work when I back onto the main fragment, because he was just hidden, and popBackStack doesn't show it,. – Neodigi Apr 16 '15 at 09:48
  • Well, I might be wrong but I see that you are removing the fragment and then showing the next one (as in Case:1). Try using replace to change the fragment (as you have used in Case:0) – kerry Apr 16 '15 at 10:26
  • This works but that was what I was trying to explain, I do not want to replace it because if I do so, the drawer will lag, that's why I only hide/show it – Neodigi Apr 16 '15 at 12:00
  • 1
    In my opinion, to add a dummy fragment just to make sure that the drawer does not lag points to the fact that there is some problem in the code. One can always optimize the app/code using other techniques rather than using a way like this. You may try to load the fragment after the drawer is closed or something like that. That would be a much better solution. – kerry Apr 16 '15 at 12:48
  • I totally agree with you, and this is what I did finally, using a I changed the fragment with a handler with some delay, I removed the dummy fragment as I also feel this was a terrible workaround, but I'm still curious as to how can I make this work. handling the backstack has always been a pain for me :D – Neodigi Apr 16 '15 at 12:56