0
I’m trying to implant a Navdrawer in my app but the example that I saw uses Fragments instead of Activity that I was already using, I found a block that deals with the Fragments.
Part of the code that uses Fragments:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
I wonder if there is a way to make Navdrawer work with Activity or if there is a way to convert the activities I have to Fragment (must be hard work) so I can deploy Navdrawer in my app.
Thanks in advance.
you would have some link to a tutorial on how to transform my activities to Rfragments? I will have to redo all my work?
– Allan Chrystian
@Allanchrystian, the Lifecycle of an Activity and a Fragment are similar... So, although you will have to port the code, the job will be more time to use the fragments in the application than when to port the code to a Ragment. I suggest you make the port yourself so you can get acquainted with Fragments. But here are two links: https://prezi.com/fh2uslbr1xs3/the-fragment-transition/ and https://github.com/OmniDebt/OmniDebt-Android/wiki/Convert-Activity-to-Fragment
– igor.araujo
Complementing and not wanting to be boring, when creating a new project with navdrawer, the code comes all commented, you can read it and easily manage to add fragments. where in the code already comes 1 fragment as subclass as example, just copy and rename, and if you take a better look at the code you can see where the Fragment is called and created. I learned so to use the navdrawer.
– Skywalker
Thanks, I’ll take a look
– Allan Chrystian