Home > Back-end >  Going to Another Activity (Android)
Going to Another Activity (Android)

Time:12-23

Sorry im new to android programming and i want to ask on how to go to another activity

Button(onClick = {
    val navigate = Intent(this@MainActivity, TitleCard::class.java)
    startActivity(navigate)
}) 

The code works in MainActivity but it doesnt work in navigation drawer.. Please help

CodePudding user response:

try this.

Button(onClick = {
    val intent = Intent(this, SecondActivity::class.java)
 startActivity(intent)
}) 

CodePudding user response:

Try below code

NavController navController = Navigation.findNavController(this, R.id.nav_host_home);
    NavigationUI.setupWithNavController(navigationView, navController);

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            int id = menuItem.getItemId();
            if (id == R.id.nav_share) {
                Toast.makeText(getApplicationContext(), "nav_share", Toast.LENGTH_SHORT).show();
            }
            NavigationUI.onNavDestinationSelected(menuItem, navController);
            drawerLayout.closeDrawer(Gravity.RIGHT);
            return true;
        }
    });
  • Related