0

I have general questions about BottomNavigationView. I would like to have a BottomNavigationView in each of my Activities in an App for ordering something (e.g. food). It should have 4 buttoms:

  • Back
  • Info
  • Stats
  • My Orders

With 'Back' the app should just go back to the previous activity. The buttoms 'Stats' and 'My Orders' should switch to a persistent activity that should not be destroyed when not being displayed. 'My Orders' should display the last orders. The buttom 'Info' should only display some information about the current item or current menu (depending from which activity it is called). So basically I have 2 questions:

  1. Should the Activities 'Info', 'Stats', and 'My Orders' be real Activities or just Fragments? Normally I think that at leat 'Stats', and 'My Orders' should be real Activities as they are persistent. But in many BottomNavigationView only Fragments are used?
  2. How can I pass content information to the Activity/Fragment 'Info'. This Activity/Fragment should display information based on the Activity is was called from. Let's say the Activities are different dishes. Do I have to create a separate Info-Activity/Fragment for each dish? Or can I somehow define a dynamic Activity/Fragment that displayes information based on the current Activity?

I'd appreciate every comment and I'd really appreciate your help.

VanessaF
  • 515
  • 11
  • 36

1 Answers1

1
  1. The recommended approach is Single Activity and Multiple fragments. You can do this using Jetpack's Navigation Component
  2. In case you need to pass data from an Activity/Fragment to the new calling Fragment, it can be done by setting arguments on the calling fragment and then getting it on the called fragment. If there is something which requires to be dynamic, for example- dishes fragment, make a single fragment and common layout and load the data dynamically from the backend.

For Setting Arguments, this should help

How to pass a variable from Activity to Fragment, and pass it back?

Note: You can use fragment without using Navigation Components but you have to use FragmentManager and FragmentTransaction and also have to maintain the Backstack by yourself which could be quite complicated

  • Thanks Dev for your answer. Regarding to 1) I do not understand what you mean by "The recommended approach is Single Activity and Multiple fragments. You can do this using Jetpack's Navigation Component". So I should use activities that contain multiple fragments? Regarding to 2) What do you mean by "load the data dynamically from the backend". As far as I understand I do not have a backend or a database. I'd appreciate further comments. – VanessaF Aug 11 '20 at 19:19
  • 1
    1)```You should watch this video``` https://m.youtube.com/watch?reload=9&v=2k8x8V77CrU ```to understand better about Single Activity and Navigation Components``` and 2)regarding the ```backend thing```, ultimately you'll be working with apps that talk to servers and get data from the server, databases/files, from your phone or clouds. Nothing is static, data has to be loaded from network or database or files. That is why, for showing same type of data, example - menu of a restaurant, you should make a single fragment which has common UI for all menus and show the data accordingly – Debarshi Bhattacharjee Aug 11 '20 at 19:57
  • 1
    1) Thanks Dev for your answer and help. I will watch the video later. Hopefully it can help me with answering whether to use fragments or activities for persistent activities. 2) Okay, so you would advice me to use a fragment with dynamic content (and not an activity). So I only have to define the fragment once, right (as an XML layout file and a Java class file)? – VanessaF Aug 11 '20 at 20:01
  • **Let me give you a scenario** - let's assume you have ```multiple restaurants lists``` as backend data, each restaurant has their own list of ```dishes```. How would you show these things in your app? Make individual activity or fragment? **No**. To show the list of each restaurant, first create a fragment and inside the fragment use ```RecyclerView```. Now to show dishes of ```each Restaurant, Make a common Fragment for dishes, design its UI, and on tap of a particular Restaurant``` from the ```RecyclerView``` just call the Dish Fragment & load the dish data accordingly. It's that simple. – Debarshi Bhattacharjee Aug 11 '20 at 20:11
  • 2) The content or data will be available from a server, for common use cases, example- showing menu or dishes, you just need to show the data of current Restaurant on the same fragment. – Debarshi Bhattacharjee Aug 11 '20 at 20:15
  • 1
    Thanks Dev for your answers and help. 1) Why would you use a fragment for the restaurants and the dishes in your example and not an activity? For the dishes I have already created an activity. What advantages does a fragment have compared to an activity. I thought fragments are kind of a window that gives you additional information. For real functunality of persistents classes I thought activities are better. 2) In my case the content or data will not come from a server. It will be imbedded in the app itself (I do not use a server at all). – VanessaF Aug 12 '20 at 18:05
  • 1)It's not that you shouldn't use many activities. You can. But if there's option for fragments, you should go for fragments as fragment also help to avoid code repetition. **You should read this** https://stackoverflow.com/questions/10478233/why-fragments-and-when-to-use-fragments-instead-of-activities. 2) Even if your data comes from database in your or embedded inside your app, you can use Fragments. And finally, its totally upto you. The recommended approach is using Many Fragment to avoid code repetition and other things. – Debarshi Bhattacharjee Aug 12 '20 at 18:42
  • Thanks for all the nice comments and your help. I will try to read and understand them. I accepted your answer to this question – VanessaF Aug 12 '20 at 19:32
  • Glad I could help. :-) – Debarshi Bhattacharjee Aug 12 '20 at 20:23
  • Hi Dev. I watched the video you gave me the link to but it was not helpfull at all and it did not answer my questions, as this video is for advanced developers not for beginners as I am (further Kotlin is used in the video and I don't use Kotlin but Java). Also the other questions did not help me. I still do not understand why I should not use Activities but Fragments. How do I avoid code repetition by using Fragments as you said? – VanessaF Aug 15 '20 at 14:15
  • In my app I made a activity for every subgroups of dishes like Maindishes, Sweets, Drinks etc.. When I use fragments, how can I do this? I have different ImageButtons in the Activities so you get a better overview of the subgroups by using Activities. When using Fragments its get way more complicated and confusing. – VanessaF Aug 15 '20 at 14:19
  • In total how many activities are you using in your app? – Debarshi Bhattacharjee Aug 15 '20 at 16:23
  • Thanks Dev for your reply. Basically I have not created all of them so far and I can't tell you for sure as this will be determined incrementally. But I suppose that I will have around 20 activities. – VanessaF Aug 15 '20 at 16:25
  • Did you read this post? https://stackoverflow.com/questions/10478233/why-fragments-and-when-to-use-fragments-instead-of-activities – Debarshi Bhattacharjee Aug 15 '20 at 16:37
  • Since you're a beginner and not able understand these things right now. It would be better for you to start coding like the way you think. At some point of time you'll start realising what's going wrong with your code and that things are getting messed up and you need to code better in order to avoid code repetitions and other things. Everyone goes through these. – Debarshi Bhattacharjee Aug 15 '20 at 16:42
  • Thanks Dev for your comment and help. Yes I read it but I do not necessarily agree to the points mentiones there. For example it says that you can use an ActionBar with Fragments. But you can also do this in a really simple way with Activities. Further it is stated that the 'Back' buttom is handled by the FragmentManager. But why should that be an advantage? You can just add a back buttom and tell the activity where to go back and even without this the app goes back when pressing "back" on your phone. – VanessaF Aug 15 '20 at 16:45
  • I think the jetpack components are really complex for beginners and it takes much time to undestand how to handle them. I think normally it is better to keep things simple and not increasing the complexitiy without major benefits. – VanessaF Aug 15 '20 at 16:47
  • All your questions are legitimate for now. You need to start coding in order to understand the concepts and points mentioned above. Once you start coding and keep on making apps, you'll have an idea of what's happening and how it can be improved. – Debarshi Bhattacharjee Aug 15 '20 at 17:32
  • Thanks Dev for your answer. Do you know a good tutorial for learning the Jetpack Navigation components and how to use fragments? – VanessaF Aug 16 '20 at 10:31
  • You should look for ```google codelabs for Navigation Component```. Also there are many codelabs provided by google to learn android development easily. Complete courses are there. You should search for them.. – Debarshi Bhattacharjee Aug 16 '20 at 18:26
  • Hi Dev, would you only use Fragments in your app and only one single activity? What would be the purpose of the activity and how would the XML layout file look like? – VanessaF Aug 20 '20 at 15:07
  • 1
    Yes I would use single activity and multiple fragments. My Main activity XML file would had the ```NavHost Fragment``` that acts as a```Container``` for other Fragments, in case using ```Navigation Components```. And all other individual Fragment's XML files would hold the UI for respective screens that I would want the user to show in my app. And I would navigate between these different Fragments. All XML files have Class files associated with it. XML holds the UI part and the respective Classes adds the functionality. Your XML file and Class both together makes the UI and functionality. – Debarshi Bhattacharjee Aug 20 '20 at 16:02
  • Thanks Dev for your answer and effort. Would your XML file for the activity just an empty file with the NavHost fragment? So just somenthing like this:https://developer.android.com/reference/androidx/navigation/fragment/NavHostFragment. Would you also usw a FrameLayout tag? – VanessaF Aug 21 '20 at 10:05
  • 1
    It depends. Various layouts are used for various **use cases**. Sometimes developers may want to include ```BottomNavigationView``` or ```DrawerLayout``` along with ```NavHostFragment``` in their activity. It totally upon the developers about what they want to include in activity XML file. But one thing is for sure if you're using ```Navigation Components``` then your activity file must have ```NavHost Fragment```. **I suggest you to first download Codelab and other projects, look at their code and get an idea first**. The more you look at other's code, the more you learn and improve. – Debarshi Bhattacharjee Aug 21 '20 at 15:25
  • Thanks Dev for your answer and help. I am aware of the fact that every fragment should have a different XML layout file depending on what you intend to display in your frontend. However my question was about the XML layout file of the one activity my app consists of. This layout file can't be changed as their is only one activity. Because of this I think it necessarily has to be an empty layout because it serves as a container for the different fragment layouts of the app. Am I misunderstanding something? – VanessaF Aug 22 '20 at 10:08
  • 1
    Not necessary that your activity has to been an empty container. What necessary is that it should atleast have the Host Fragment. The host Fragment serves a the container for all other fragments. The host fragment is just a ```fragment tag``` or a ```fragmentContainerView```. – Debarshi Bhattacharjee Aug 22 '20 at 12:06
  • Thanks Dev for your comment and your effort. So the HostFragment should be in the one activity of your app. And the HostFragment should be empty, right? – VanessaF Aug 23 '20 at 08:13
  • 1
    Yes. The Host fragment is just a ```FragmentTag``` so by default it's empty but make sure the necessary attributes are provided. – Debarshi Bhattacharjee Aug 23 '20 at 09:34
  • Thanks Dev for your comment and help. By 'necessary attributes' you mean attirbutes about the constrainedLayout to embed it into (or Relative Layout)? – VanessaF Aug 23 '20 at 09:40
  • 1
    Attributes are the properties of the tag . Ex-```id, name, styles, and there are many more ```. ***Anything that goes inside your tag are the properties or attributes of that tag***. By tag I mean, the XML tags- ConstraintLayout, View, RelativeLayout, etc etc. – Debarshi Bhattacharjee Aug 23 '20 at 09:59
  • Thanks Dev for your answer and help. I have a lot of activities in my app (about 20; most of them are very similar; they just have a different heading and different number of Imagebuttoms). Would you recomment me to convert all of them in Fragments? I am having problems converting the activities into fragments (the layouts are overlapping when I display them). I think it is quite time-consuming and not easy to convert the activities into Fragments. – VanessaF Aug 23 '20 at 10:27
  • Thanks for your answer Dev and your effort. Can the HostFragment also be a FrameLayout tag rather than a FragmentTag? Using a FragmentTag (like the one from this example https://developer.android.com/reference/androidx/navigation/fragment/NavHostFragment) produces error messages and a faulty layout. When using the FrameLayout tag is looks okay. – VanessaF Aug 23 '20 at 10:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220303/discussion-between-dev-bhattacharjee-and-vanessaf). – Debarshi Bhattacharjee Aug 23 '20 at 10:53
  • Hi Dev. Thanks for the useful chat and your comments. I have a follow up question: Would you not use the BottomNavigationGroup but rather the Navigation components? – VanessaF Aug 24 '20 at 08:33
  • Sorry, I never used BottomNavigationGroup so I can't say anything about that – Debarshi Bhattacharjee Aug 24 '20 at 12:09
  • Okay thanks Dev for your answers. I will try to use the Jetpack Navigation components. – VanessaF Aug 24 '20 at 13:46