My navigation Drawer no longer goes to my screens, just a logout button I made, the others do not call the other screen

Asked

Viewed 71 times

-1

public class MainActivity extends AppCompatActivity {
 private AppBarConfiguration mAppBarConfiguration;


    ImageView imageView_Carrinho;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        imageView_Carrinho = findViewById(R.id.imageView_Carrinho);


        imageView_Carrinho.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, CarrinhoAdapter.class);
                startActivity(i);
            }
        });

       // Cria referencia para toda a area do navigation drawer
        DrawerLayout drawer = findViewById(R.id.drawer_layout);

        //Cria referencia para a area de navegação
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.

        NavigationView navigationView1 = findViewById(R.id.nav_sair);


        //Define configurações do navigation drawer
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();

        //Configura area que ira carregar os fragments
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

        //Configura menu superior da navegação
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);

        //Configura
        NavigationUI.setupWithNavController(navigationView, navController);


        navigationView.setNavigationItemSelectedListener
                (new NavigationView.OnNavigationItemSelectedListener() {

                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        switch (menuItem.getItemId())
                        {
                            case R.id.nav_sair:
                                //faça qualquer ação aqui
                                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                                finish();
                                startActivity(intent);

                                break;
                        }
                        return true;
                    }
                });
    }

    /**  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }**/

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
    

}
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer"
            android:background="@drawable/degrade_menu2"
            app:itemTextColor="@color/Branco"
            app:itemIconTint="@color/Branco"
           />
    
    </androidx.drawerlayout.widget.DrawerLayout>```

  • Tell which part of the app each code refers to. In the title just enter the problem, leave to give the details in the question.

  • sorry, I’m new r

1 answer

0

Eai partner, jewel? I hope I can help you! Generally, when we use the Drawer layout feature, we have an xml file, called mobile navigation or the name given by you, which is possible to define the Fragments we have and their navigation indications (as the name says)This allows you to click on the items of the navigation Drawer and go to other Ragments. With my experience, which is not very large, I assume that with Drawer layout, it is easier to move between Ragments, but if it is an interaction Fragment -> Activity, try to insert in your code the feature onNavigationItemSelected() in its main class, So, it is presumed to solve your mistake! I hope I’ve helped!

Browser other questions tagged

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