Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Fragment before Navigation Drawer closes

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?

like image 984
user1908375 Avatar asked Aug 28 '13 13:08

user1908375


People also ask

How to implement fragment loading in navigation drawer?

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.

Why should I use fragments instead of code?

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.

Can I use a relativelayout/linearlayout for the navigation drawer?

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>

Can I use fragments to solve the view logic problem in codepath?

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.


Video Answer


3 Answers

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;
 }
like image 194
user1908375 Avatar answered Oct 24 '22 15:10

user1908375


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()

like image 23
Arvin Avatar answered Oct 24 '22 14:10

Arvin


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.

like image 39
iamreptar Avatar answered Oct 24 '22 16:10

iamreptar