Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. MainActivity or MainFragment?

MainActivity or MainFragment?

Scheduled Pinned Locked Moved Android
androidjavacomxmlquestion
1 Posts 1 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I have just created a default Android Studio project with navigation drawer. The project was created with:

    MainActivity.java
    NavigationDrawerFragment.java

    and

    activity_main.xml
    fragment_main.xml
    fragment_navigation_drawer.xml

    does this mean my startup will be an activity not fragment? Do I need to change it? I am trying to set the item actions in the navigation drawer like this:

    private void selectItem(int position) {
        mCurrentSelectedPosition = position;
        Fragment fragment = null;
    
        switch (position) {
            case 0:
                fragment = new WeatherFragment();
                break;
            case 1:
                fragment = new WeatherFragment();
                break;
            case 2:
                fragment = new WeatherFragment();
                break;
    
            default:
                break;
        }
    
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
    
        if (mDrawerLayout != null) {
            // android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
            // fragmentManager.beginTransaction().replace(R.id.fragment\_main, fragment).commit();
    
            // mDrawerList.setItemChecked(position, true);
            // mDrawerList.setSelection(position);
            // getActionBar().setTitle(mNavigationDrawerItemTitles\[position\]);
            // mDrawerLayout.closeDrawer(mDrawerList);
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }
    

    so what this line should be:

    fragmentManager.beginTransaction().replace(R.id.fragment_main, fragment).commit();

    replace an activity or fragment. Thanks, Jassim

    Technology News @ www.JassimRahma.com

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups