Navdrawer using activities instead of Fragments

Asked

Viewed 180 times

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.

1 answer

3


Unfortunately it is not possible to implement a Navigationdrawer using activities the way you want. In reality, what you can do is fire attempts to start your Activities when a Navigationdrawer item is clicked. But I don’t think behavior will be what you want.

Having said that, I suggest you turn your activities into fragments. In fact, before creating an Activity, always ask yourself if you really need it and/or you can’t trade it for a fragment. Fragments are very powerful and help you modularize and reuse application components.

  • you would have some link to a tutorial on how to transform my activities to Rfragments? I will have to redo all my work?

  • 1

    @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

  • 1

    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.

  • Thanks, I’ll take a look

Browser other questions tagged

You are not signed in. Login or sign up in order to post.