Search code examples
androidandroid-fragmentsnavigationnavigation-drawerandroid-fragmentactivity

Want to move from navigation drawer activity to another screen using fragments to show drawer across all screens


I'm trying for last two days to add fragment next to my drawer activity to get navigation drawer visible across the whole application. I have tried several ways from stackoverflow and many others but still no success. and after that i have to move to 2nd fragment from 1st fragment and so on till the need for navigation drawer. I want to replace entire view except drawer when i move from from my activity to any fragment. Each fragment have its own layout.xml like an activity(Linear/Relative layouts as parent in them).

Drawer avtivity:

 public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Button btnfragOne = (Button) findViewById(R.id.btnfrag_one);
    btnfragOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            FragOne fragment = new FragOne();
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frag2, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();

        }
    });

}

1st Fragment class:

public class FragOne extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.frag_one, container, false);
}

// 2nd Fragment class:

public class FragTwo extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.frag_two, container, false);
}

Solution

  • Simply for this task you have to override onNavigationItemSelected method in your Activity, which return id of selected fragment on NavigationDrawer.

    Try this,

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
    
        //calling the method displayselectedscreen and passing the id of selected menu
    
        displaySelectedFragment(item.getItemId());
    
        return true;
    }
    

    Now displaySelectedFragment,

    private void displaySelectedScreen(int itemId) {
    
        //creating fragment object
        Fragment fragment = null;
    
        //initializing the fragment object which is selected
        switch (itemId) {
    
            case R.id.your_fragment_one_id:
                fragment = new FragOne();
                break;
    
            case R.id.your_fragment_two_id:
                fragment = new FragTwo();
                break;
        }
    
        //replacing the fragment
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.main_layout_id_which_is_to_be_replace, fragment);
            ft.commit();
        }
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.your_drawer_layout_id);
        drawer.closeDrawer(GravityCompat.START);
    }
    

    Edit -- If you want navigate from FragOne to FragTwo. Try this,

    Create a method in your Activity,

    public void showFragTwo(){
    
        FragmentManager manager = getSupportFragmentManager();
        FragTwo frag = new FragTwo();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.your_layout_id_which_is_to_be_replace, frag);
        transaction.commit();
    
    }
    

    Then in your FragOne when you want to start FragTwo, call startFragTwo method from Activity as,

    ((YourActivity)  getActivity()).showFragTwo();