0

I use a One-Activity-Multiple-Fragments approach and the JetPackNavigation with a NavGraph. I have added a BottomNavigationBar to navigate to 2 Fragments which works as it should. Now I want to put a backbuttom into the BottomNavigationBar with the intensions that if someone presses it, it should go back to the very last fragment visited. I want to know if something like this is possible and if I have to set the connections in the NavGraph from all Fragments to the other Fragments such that it can navigate back? The problem is that my NavGraph is quite large and contains many fragments.

Here you see a screenshot from the NavGraph where I indicated the 2 Fragments for the Navigation in the BottomNavigationBar (which work quite well).

Further, here you see the XML code for the BottomNavigationBar.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <item
        android:id="@+id/Back_BottomNavigation"
        android:icon = "@drawable/ic_baseline_arrow_left_24"
        android:title = "Back" />

    <item
        android:id="@+id/FR_LanguageSelection"
        android:icon = "@drawable/ic_add_circle_full"
        android:title = "@string/Language" />

    <item
        android:id="@+id/Fragment1"
        android:icon = "@drawable/ic_add_circle_full"
        android:title = "Fragment1" />





</menu> 

Do you know if I can implement the back-buttom such that it will go back to the last displayed Fragment? I'd appreciate every comment.

NavGraph

Does nobody have an idea how I can do this? I read quite often that the Jetpack Navigation is good for handling the backstack. Is this true and how can I do this?

Update: I inserted, as adviced in one answer, a Listener for the BottomNavigationView into the MainActivity (which hosts all the Fragments using the NavHostFragment). Now the back navigation works as it should. However, the normal Jetpack navigation using the other Bottoms of the BottonNavigationBar does not work anymore. When I click on the other bottoms, nothing happens (before adding the Listener in the Main Activity it was working perfectly). Do you have any idea, how I can solve this problem? Here is the code of the newly added Listener in the onCreateMethod of the MainActivity class:

//These commands were there before
        final  NavController navController = Navigation.findNavController(this,
                R.id.navHostfragment);
        NavigationUI.setupWithNavController(binding.bottomNavigation, navController);

//These are the new commands
 binding.bottomNavigation.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.Back_BottomNavigation:
                                navController.navigateUp();


                        }
                        return true;
                    }
                });

Any ideas about how to tackle this problem?

VanessaF
  • 515
  • 11
  • 36
  • What about findNavController().navigateUp()? – ming chen Feb 20 '21 at 09:49
  • Thanks ming for your answer. Where shall I write this code? I do not have a Java file for the BottomNavigationBar (I just create it in the MainActivity), as the navigation works automatically when you have specified the destinations in the BottomNavigationBar – VanessaF Feb 20 '21 at 09:54

1 Answers1

1

documentation

set the behaivor you want in BottomNavigationView's OnNavigationItemSelectedListener

yourBottomNavigationView.setOnNavigationItemReselectedListener(object : OnNavigationItemSelectedListener{
@override fun onNavigationItemSelected (MenuItem item){
  when(item){
    //insert your condition to match your requirement
  }
}

})
ming chen
  • 550
  • 4
  • 13
  • When I copy the code from your previous link into my main actvity I get the following error "lass 'Anonymous class derived from OnNavigationItemSelectedListener' must either be declared abstract or implement abstract method 'onNavigationItemSelected(MenuItem)' in 'OnNavigationItemSelectedListener'" – VanessaF Feb 20 '21 at 10:08
  • So the question is where (in which Fragment or Activity) and how to implement the back-button fuctionality – VanessaF Feb 20 '21 at 10:09
  • Thanks ming for your answer. Any comments on my last comment? I'd highly appreciate every further comment from you. – VanessaF Feb 21 '21 at 08:51
  • @VanessaF implement listener where you bind your BottomNavigationView, mostly is in your Activity. – ming chen Feb 22 '21 at 01:23
  • Thanks ming chen for your answer. I implemented what you suggested and the back buttom now works. HOWEVER: the other bottoms in the BottomNavigationView do not work any longer. As said before I use the Jetpack Navigation components and when using them the BottomNavigationBar navigates 'automatically' when just specyfying the correct names in the menu files. However, when using your approach I have the feeling, that this 'automatica' navigation is being eliminated. So my question is how to combine them? – VanessaF Feb 27 '21 at 10:02
  • So basically the question is how can I only have the behaviour of your suggested approach for only one bottom (back buttom) of the BottomNavigationView and the other bottoms should just use the 'automatic navigation' of Android Jetpack – VanessaF Feb 27 '21 at 10:07
  • Thanks ming for your answer. Any comments on my last comments and the update? I'd highly appreciate every further comment from you. – VanessaF Mar 02 '21 at 18:00
  • @VanessaF Sorry I was on a vacation. This approach overrides the default behavior of onNavigationItemSelected. You should implement every each behavior of the button on the BottomNavigationView. – ming chen Mar 03 '21 at 02:25
  • Thanks ming chen for your answer and effort. I really appreciate it. Is there no way to combine those two approaches? The problem is that I use the Jetpack Navigation to navigate from one fragment to another fragment. I do not know how I can navigate to the other fragments using your approach. I can only use "navController.navigateUp();" but I do not know how to navigate to another fragment. – VanessaF Mar 03 '21 at 18:26
  • Normally I use something like this inside the fragments to navigate: String argument = menuList.get(position).getItemName(); Fragment_1Directions.ActionFragment_1ToFragment_2 action = Fragment_1Directions.actionFragment_1ToFragment_2 (argument); Navigation.findNavController(getView()).navigate(action); --> but I can't use this with your approach – VanessaF Mar 03 '21 at 18:28
  • @VanessaF The official way is create an action in navigation graph from one target to another, and then you can call like navController.navigate(R.id.your_action_to_your_target). – ming chen Mar 04 '21 at 02:44
  • Thanks ming chen for your answer and effort. I really appreciate it. How can I implement the offical way in combination with you suggested approach for the back-button? Basically when not using your approach, the navigation worked perfectly except the back-button using the navGraph. With your suggested approach the navigation does not work anymore but just the back-button works – VanessaF Mar 04 '21 at 18:27
  • @VanessaF You have done overriding one item, just keep overriding the others. – ming chen Mar 05 '21 at 01:58
  • Thanks ming chen for your comment. Well the problem is that I can't navigate to the other fragments using the Jetpack Navigation approach. How can I do that? You wrote "The official way is create an action in navigation graph from one target to another, and then you can call like navController.navigate(R.id.your_action_to_your_target)". Basically I use a single-activity-multiple-fragment approach. How shall I define the tagets in the NavGraph? – VanessaF Mar 05 '21 at 18:31
  • The idea of the BottomNavigationBar is that I can jump from every fragment to some specific fragments. Do I have to make a connection from every fragment to each of those specific fragments? I have about 20 normal fragments and 4 specific ones. That would mean I need 20*4=80 connections in the NavGraph which makes the NavGraph quite full of connections. Normally - without using your suggested appraoch - I don't need any connection to navigate using the BottomNavigationBar (while having the problem that the Back-button can't be used). So I am quite confused as to how to combine the 2 methods. – VanessaF Mar 05 '21 at 18:37
  • In this case I will suggest you using nested navigation instead wrapping all fragment in one graph. https://developer.android.com/guide/navigation/navigation-nested-graphs – ming chen Mar 08 '21 at 01:16
  • But you can still navigate directly to the fragment you want by the .navigate(R.id.your_fragment) function. – ming chen Mar 08 '21 at 01:18
  • Thanks ming chen for your answer and effort. I really appreciate your help. Basically with your last suggested code".navigate(R.id.your_fragment) " it works as intended :-). However there is one small problem that I am still facing. If you click on one item on the BottomNavigationBar, this symbol remains highligted (both the icon and the text become a little bit bigger), until I press another bottom on the BottomNavigationBar. So even when I am not any more in the corresponding fragment of the symbol in the BottomNavigationBar the icon remains highlighted. Any idea how I can tackle this issue. – VanessaF Mar 10 '21 at 18:36
  • @VanessaF If you navigate to other fragment which have corresponding bottomNavigation tab through action in your fragment, sure you will encounter this situation. In app designing aspect, ButtonNavigation is for you to separate different function part of your app, fragment under different tabs shouldn't have direct link with each other, and also it's the reason I suggest you to separate them into different nav graph. On implementation aspect, if you are sure this is what you want, use setSelectedItemId(int itemId) instead of using action on those fragment. – ming chen Mar 11 '21 at 02:33
  • Thanks ming chen for your answer and effort. What do you mean by "if you are sure this is what you want, use setSelectedItemId(int itemId) instead of using action on those fragment."? Basically regarding navigation this is what I want. When I press on one icon in the BottomNavigationBar then it navigates to the correct destination. But regarding highliting this is not what I want. When I am at a certain fragment it should be highlited in the BottomNavigationBar. But when navigating to another Fragment (which is not in the bar) the initially highlited icon should not be highligthed any more – VanessaF Mar 12 '21 at 18:08
  • Further related question: Is it also possible to click on a botton in the BottomNavigationBar and navigate from Fragment_A to an certain Fragment_Bottom1 and the Fragment_Bottom1 should display some text based on the Fragment that 'called' it? So in this case Fragment_Bottom1 should display a text based on Fragment_A. And when again clicking on the same icon in the BottomNavigatonBar the App should go back to the Fragment_A again from the Fragment_Bottom1. So in this case the icon in the BottomNavigationBar should just serve as a button for additional context sensitive information. – VanessaF Mar 12 '21 at 18:12
  • @VanessaF Sorry mate, I'm gonna have to ask you to google it, since it seems you haven't do any research before throwing these question. – ming chen Mar 15 '21 at 01:59
  • Thanks ming for your answer. Basically I googled it and I have been trying for many many hours by now. Unfortunately without success this is why I asked this question. And as said before, now the navigation of the BottomNavigationBar works, the only problem is that the symbol remains highlighted. I suppose there has to be a way how to get rid of that because I have used apps that also have a BottomNavigationBar and where the symbol does not remain highlighted after navigating to another fragment that is not part of the Bar. Any idea how I can achieve this? I'd appreciate any further comments – VanessaF Mar 17 '21 at 18:20
  • Thanks ming for your comments. Any further comments on my last comment? I'd highly appreciate any further comments from you. – VanessaF Mar 19 '21 at 17:20
  • I accepted and upvoted your answer ming. Thanks for your tremendous help. I really appreciate it. – VanessaF Mar 21 '21 at 08:12