I have implemented a navigation drawer and I want to load my fragment before the navigation drawer closes. Currently, the fragment loads in parallel with the drawer closing, so if the fragment is heavy the user interface hangs for a bit.
The code I have is:
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragmentProfile);
ft.commit();
drawerLayout.closeDrawer(drawerNaviListView);
}
}
How can I change this so that I see my fragment loading (in background) first and when it has finished loading, the navigation drawer closes?
That’s all you need to do to implement fragment loading in your Navigation Drawer. What the user taps a menu item, the drawer will slide back in smoothly and the new fragment will have already started / finished loading. This also prevents any possible jankiness that you could see when launching a new activity.
The bulk of the code is the same regardless of if you’re showing one fragment or both which is part of the reason you use fragments, so you don’t have to reproduce code in multiple places. The problem I ran into with the Navigation Drawer sample is again that it doesn’t really demonstrate how you should do navigation inside of it.
Although many navigation drawer examples show how fragments can be used with the navigation drawer, you can also use a RelativeLayout / LinearLayout if you wish to use the drawer as an overlay to your currently displayed Activity. Instead of <FrameLayout> you can substitute that for a <LinearLayout>
Additionally, by implementing CodePath’s example, too much of the view logic would live in the hosted activity, so I set out to see if I could use fragments to solve this problem. Basically, we will have a master fragment that will be used for the drawer view and a detail fragment for the main view.
My solution was to load fragment AFTER drawer is closed: actually call loadFragment method inside onDrawerClosed
public void onDrawerClosed() {
// assure the request comes from selecting a menu item, not just closing tab
if (selectedTab )
selectItem(mSelectedFragment);
selectedTab = false;
}
Try to load after the drawer is closed Use handler to create a delayed execution. So that there won't be any hang when the drawer closes
Inside the method onItemClick()
, use the below code :
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragmentProfile);
ft.commit();
}
};
handler.postDelayed(runnable, 500); // here 500 is the delay
// other way of doing it is
create an AsyncTask and in doInBackground() method start the fragment transaction and close the drawer in onPostExecute()
DrawerLayout.DrawerListener can be used to monitor the state and motion of drawer views. Avoid performing expensive operations such as layout during animation as it can cause stuttering; try to perform expensive operations during the STATE_IDLE state. Source.
In other words, Android recommends that you wait for your drawer to close before swapping Fragments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With