To call up a new Activity:
Create a new Activity
with the name of MainActivity2
and enter this code below in your onNavigationItemSelected
method of your MainActivity
:
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
To call a new Fragment:
Create a new Fragment
with the name of novoFragment
and enter this code below in your onNavigationItemSelected
method of your MainActivity
:
novoFragment fragment = new novoFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();
** Important Notice **
In order for this fragment
call to complete successfully (work), you need to follow this most complete post on the subject: 3-point menu in all Activities with the default Navigation Drawer
In your MainActivity - Solution in Practice:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// abrindo um novo fragment
novoFragment fragment = new novoFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();
} else if (id == R.id.nav_gallery) {
// abrindo um nova activity
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}