To call a new Activity():
Create a new Activity
by the name of MainActivity2
and insert this code below into your method onNavigationItemSelected
of his MainActivity
:
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
To call a new Fragment(Fragment):
Create a new Fragment
by the name of novoFragment
and insert this code below into your method onNavigationItemSelected
of his MainActivity
:
novoFragment fragment = new novoFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();
** Important Notice **
Why did you call fragment
if it concludes successfully (work), it is necessary to follow this more complete post on the subject: 3-point menu in all Activities with standard 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;
}
You know how to open other
Activity
s and is having trouble just with this case or you do not know how to openActivity
s?– Pablo Almeida
can’t open yet
– Marcos MT6