Navigation Drawer without interaction when clicked

Asked

Viewed 277 times

2

I’m using the default menu Navigation Drawer and complemented the method that already comes in the project to open my activities(Atividades). I have already created the classes and called each of them as image below, but when I click on a menu item, and it does not change windows.

erro

and here’s one of my classes:

public class SecondActivity extends AppCompatActivity {
private Button share;
private Toolbar mToolbar;
private Toolbar mToolbarBottom;
private Button action_settings;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);


    mToolbarBottom = (Toolbar) findViewById(R.id.inc_tb_bottom);
    mToolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            Intent it = null;

            switch(menuItem.getItemId()){
                case R.id.action_facebook:
                    it = new Intent(Intent.ACTION_VIEW);
                    it.setData(Uri.parse("https://www.facebook.com/PrefeituraMunicipalDeJaguaruana"));
                    break;
                case R.id.action_youtube:
                    it = new Intent(Intent.ACTION_VIEW);
                    it.setData(Uri.parse("http://www.jaguaruana.ce.gov.br/"));

            }

            startActivity(it);
            return true;
        }
    });
    mToolbarBottom.inflateMenu(R.menu.menu_bottom);

    /*mToolbarBottom.findViewById(R.id.iv_settings).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(SecondActivity.this, "Settings pressed", Toast.LENGTH_SHORT).show();
        }*/

}


@Override
protected void onResume() {
    super.onResume();

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        mToolbar.setBackgroundResource(R.drawable.toolbar_rounded_corners);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_second, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == android.R.id.home){
        finish();
    }

    return true;
}
  • try to change to Intent intent = new Intent(this, MainActivity.class); startActivity(intent);

1 answer

1


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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.