简体   繁体   中英

onNavigationItemSelected not registering user selection

I have created a new Android project starting from the NavigationDrawer activity template, so the following code is as close to default behavior as possible.

Currently my navigation drawer includes the three default entries: Home, Gallery and Slideshow. When I click on any of them, the corresponding fragment is displayed. What I would like to do is call an external activity when these items are pressed, get some results, and then display the results in the fragment (instead of calling the fragment directly).

To do this, this is what I have done:

  • I have implemented NavigationView.OnNavigationItemSelectedListener in my main activity
  • I have added the listener to the navigation view: navigationView.setNavigationItemSelectedListener(this);
  • I have overridden the onNavigationItemSelected method in my main activity

Here is the complete code for MainActivity.java (I haven't touched any other project files so far):

package com.example.test;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private AppBarConfiguration mAppBarConfiguration;
    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @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();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        switch (item.getItemId()) {

            case R.id.nav_gallery: {
                Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();
            }
        }
        //close navigation drawer
        drawer.closeDrawers();
        return true;
    }
}

When I click on the Gallery menu item, the Toast message "clicked" should appear. Instead, the corresponding fragment is called. How can I make this work so that selecting a menu item calls an external activity, gets the results from it, and displays them in the corresponding fragment?

There are a couple of ways to achieve this. The easisest one is to:

  1. In your activity define a function with public access modifier so you can access it outside of your class.
void someMethodOnMainActivity() {}
  1. In your fragment:
Activity activity = requireActivity();
if (activity instanceof MainActivity) {
  ((MainActivity) activity).someMethodOnMainActivity();
}

You can also invoke onNavigationItemSelected() in your MainActivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM