Starting a new activity on navigation drawer item click

48,850

Solution 1

For each case statement you just need to specify which Activity you want to start via an Intent.

Say for example you want to start the Playboard activity when navigation_item_1 is selected.

You would add this code to that particular case.

case R.id.navigation_item_1:
    Intent i = new Intent(MainActivity.this, Playboard.class);
    startActivity(i);
    break;

Solution 2

Instead of

NavigationUI.setupWithNavController(navigationView, navController);

Do this

navigationView.setNavigationItemSelectedListener(new 
    NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
            Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
);
Share:
48,850
choloboy
Author by

choloboy

Updated on July 09, 2022

Comments

  • choloboy
    choloboy almost 2 years

    I know this is a question frequently asked, but after reading the many questions and solutions on stack overflow I am confused. I am confused with regards to Fragments and what is required to start an activity from clicking an item in the navigation drawer.

    I've checked these posts but only got confused Q1, Q2

    Could someone please explain what is required to start a basic activity from this navigation drawer item? Do I need an onClick method implemented where specified in the code? Also how does this relate to the Intent?

    Here is my MainActivity.java

    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.support.design.widget.NavigationView;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    
    public class MainActivity extends AppCompatActivity {
    
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle drawerToggle;
    NavigationView navigation;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initInstances();
    }
    
    private void initInstances() {
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.hello_world, R.string.hello_world);
        drawerLayout.setDrawerListener(drawerToggle);
    
        navigation = (NavigationView) findViewById(R.id.navigation_view);
        navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                int id = menuItem.getItemId();
                switch (id) {
                    case R.id.navigation_item_1:
                        //Do some thing here
                        // add navigation drawer item onclick method here
                        break;
                    case R.id.navigation_item_2:
                        //Do some thing here
                        // add navigation drawer item onclick method here
                        break;
                    case R.id.navigation_item_3:
                        //Do some thing here
                        // add navigation drawer item onclick method here
                        break;
                    case R.id.navigation_item_4:
                        //Do some thing here
                        // add navigation drawer item onclick method here
                        break;
                    case R.id.navigation_item_5:
                        //Do some thing here
                        // add navigation drawer item onclick method here
                        break;
                }
                return false;
            }
        });
    
    }
    
    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation_view_items, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item))
            return true;
    
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.string.action_settings) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    And here is the second activity, Playboard.java, that simply loads a background image:

    import android.app.Activity;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class Playboard extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_playboard);
        }
    }
    

    All input greatly appreciated thanks!

  • choloboy
    choloboy about 8 years
    to use intent do I need to import any library specifically? I tried this just now and have an error on intent and i
  • George Mulligan
    George Mulligan about 8 years
    There is a required import statement in the file: import android.content.Intent;. Android Studio and Eclipse have the ability to help resolve missing imports for you though.
  • Craig P
    Craig P over 5 years
    when i try this the drawer and navigation are lost, the new Activity opens but doesn't have the slider or heading etc