Skip to content Skip to sidebar Skip to footer

Passing Value From Adapter To A Bottomnavigationview Fragment In Navigation Components

The application has a BottomNavigationView on the MainActivity which deals with the navigation between Fragments. One of the Fragments has a RecyclerView in it and the items of the

Solution 1:

MapFragmentmapFragment=newMapFragment();
LatLnglatLng=newLatLng(markerObject.getLatitude(),markerObject.getLongitude());
//Create Bundle and add dataBundlebundle=newBundle();
bundle.putString("position",latLng.toString());
mapFragment.setArguments(bundle);

Here the mapFragment is just a created object that is not used in the navigation graph. i.e. it's a standalone object that is independent of the navigation, so it is not involved in the navigation as it's not the same as the current mapFragment fragment in the navGraph.

Answer :

In order to solve this, you need to pass the argument whenever you switch among fragments of the BottomNavigationView, and registering OnNavigationItemSelectedListener is a good place for that:

First allow the adapter to send the arguments back to the activity through the listener callback

So, modify the listener callback to accept a parameter

interfaceOnItemClickListenerlistener {
    voidonItemClick(Bundle args);
}

Apply that on adapter

@OverridepublicvoidonBindViewHolder(@NonNull LocationsViewHolder holder, int position) {

        //A click listener for the button
        holder.showMarkerButton.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                
                LatLnglatLng=newLatLng(markerObject.getLatitude(),markerObject.getLongitude());
                
                //Create Bundle and add dataBundlebundle=newBundle();
                bundle.putString("position",latLng.toString());
                
                //Interface to transport the click event to the MainActivity to switch Fragment in the BottomNavigationView
                listener.onItemClick(bundle);
            }
        });
}

And finally, register OnNavigationItemSelectedListener to the navView and modify the callback to accept the Bundle in activity:

publicclassMainActivityextendsAppCompatActivityimplementsOnItemClickListener {
    BottomNavigationView navView;
    
    private Bundle mapArgs;
    
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each// menu should be considered as top level destinations.AppBarConfigurationappBarConfiguration=newAppBarConfiguration.Builder(
                 R.id.navigation_home,R.id.navigation_map, R.id.navigation_locations, R.id.navigation_profile)
                .build();
        NavControllernavController= Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        
        
        navView.setOnNavigationItemSelectedListener(newBottomNavigationView.OnNavigationItemSelectedListener() {
            @OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem item) {
                navController.navigate(item.getItemId(), mapArgs);
                returntrue;
            }
        });     
        
    }

    @OverridepublicvoidonItemClick(Bundle args) {
        navView.setSelectedItemId(R.id.navigation_map);
        mapArgs = args;
    }


}

Solution 2:

When using the navigation controller, you pass in data with parameters. Go to your navigation component XML file and make the following changes;

1.Inside the navigation action, you need to add an argument.

<actionandroid:id="@+id/action_id"app:destination="@id/destinationFragment"><argumentandroid:name="paramterName"app:argType="string" /></action>

2.Add the same argument to the destination fragment

<fragmentandroid:id="@+id/coursesNavFragment"android:name="com.example.example.DestinationFragment"android:label="destination_fragment"tools:layout="@layout/destination_fragment" ><argumentandroid:name="parameterName"app:argType="string" /></fragment>

You can easily do this using the design view as well.

Now the function you call to navigate to the destination fragment expects you to pass it a parameter.

navigationController.navigate(actionSourceFragmentToDestinationFragment("text123"))

Then you can extract the bundle in the destination fragment.

valparameter= DestinationFragmentArgs.fromBundle(requireArguments()).parameterName
// parameter has the value of "text123"

Since the navigation component uses safeargs, you can pass any type of parameter that inherits from Parcellable.

Post a Comment for "Passing Value From Adapter To A Bottomnavigationview Fragment In Navigation Components"