Android sharing BottomNavigationView across app - android

I'm new to Android development and I wanted to ask what is the best solution here.
In my activity_main.xml I have some content and on the bottom there is <android.support.design.widget.BottomNavigationView>
I set some icons and created listener for it. The Navigation contains simple Settings icon, and when it's clicked it should start the SettingsActivity. This navigation shoud be everywhere in the app.
Here comes my question :
What is the best approach when I don't want to implement this BottomNavigationView in every single activity, implementing the same listener over and over again in every activity ? I've heard of using fragments instead of activity but these are now deprecated right? There must be a better way, implementing the Navigation over and over again with exact same code can't be right approach.

Here comes my question : What is the best approach when I don't want
to implement this BottomNavigationView in every single activity,
implementing the same listener over and over again in every activity
You won't need to do that since Fragments are here to help and they're not depreciated but, replaced with AndroidX (new) one: androidx.fragment.app.Fragment. Instead of implementing it over and over again, replace the new Fragment when the other item selected.
Use setOnNavigationItemSelectedListener then do your stuff.
Check the sample here.

While regular com.android.Fragment is deprecated, the support fragment class located in either androidx.fragment.app.Fragment is not deprecated, and is probably what it makes the most sense to use here.
So using the fragment approach, you will have one Activity class with the BottomNavigationView set up. When the user navigates using the BottomNavigationView, you'll change which fragment is being shown in your Activity. This tutorial should be a good starting point for how to enable this functionality.

Related

Navigation Architecture Component: How to use same Fragment while navigation

I have 3 fragments navigation each of them by 'Navigation.navigate' making network API to reload and viewpager to set again. I wanted to use the same fragment as it is, which was open already.
I am using the "Navigation Architecture Component", and I am using Navigation.navigate method
If you create your fragment each time a navigation happens you can instead use show() and hide() methods of the fragment, so whenever one of them is the visible show it and hide the other two and another way around. But if your problem is reloading data maybe you should consider other options. Like using ViewModel to store the data of the fragment. please provide more info and publish your code so we can help you better.

Common Navigation drawer for multiple activities without using fragments. Is it possible in android

I am planning to start an app from the scratch and I need to know wheather a COMMON Navigation drawer is applied through out multiple activities without using a fragment inside the activity.
I tried many examples but none worked for me. Any help should be greatly appreciated.
Yes it is possible create one base activity and implement the drawer in base activity like here. and extends that base activity
As far as I know it is not possible. I've personnaly used a single activity hosting a navigation drawer instantiating differents fragments to swipe between them.
Here if you wanna do it: http://developer.android.com/training/implementing-navigation/nav-drawer.html
The best you could have to get kind of the same result with activities is playing with transitions params in each of their class. It is kinda heavy. Use fragments for this purpose.
The following topic could bring you some work elements if you still wanna do it: Android how to add swipe Gesture on LinearLayout without onDown true
But keep in mind that it won't be as smooth as the navigation drawer with fragments.
As far it's not possible without using fragment. and why you don't want use fragment.?
Fragment is a best thing to achieve this.
Here i am attached some links.
I hope that links are use full for you.
https://www.learn2crack.com/2014/05/android-working-with-fragments.html
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

It would be correct to use the NavigationDrawer to open Activities, not fragments?

My application basically has one main activity. Within it there are three tabs, each with a fragment. Something similar to the layout of whatsapp.
To add more functionality to the application, I saw that the NavigationDrawer would be a good option. But because of my application running with a main activity with children fragments, I wonder if it would be a bad practice loading activities and not fragments, when the User clicking on any item NavigationDrawer.
Or does the best meneira would turn my MainActivity in a fragment? It would give me a great job ..
I would like to suggestions =)
You can use the NavigationDrawer to open new activities. Basically just make each of the Activities have the same-looking Drawer and the user experience will be same as if you were replacing Fragments.
However, I would discourage this. I have been working with apps that mixed Fragments and Activities for the NavigationDrawer and the outcome was problematic, especially when it came to backtracking and saving state. It did work, but required hackfixing and some illogical code.
The best practice for the NavigationDrawer is to have one "container" activity which does little more that just exchanging Fragments in a FrameLayout it holds. The rest of the logic would be in the Fragments. This way the app is easily extendable and the backstack is handled by the platform.
I prefer Fragments but there is no problem to open new Activities. Instagram does something similar. It has tabs (I know that you want to use NavigationDrawer, but this is just a example), and one on these tabs open a new Activity, with the X button to close the Activity.
Instagram Main Activity:
New Activity with close button:
The Series Guide Android application does exactly the same. It opens Activities from the Navigation Drawer. Such Activities extend the BaseNavDrawerActivity. The Activity's start and exit animations are custom to make the transition looking smoother.
I would recommend this approach since it's much more easier to manage Activities' back stack than Fragments'.
Proceed happily with opening activity as well. Make sure you put the back button on the opened activity so that user would have clear idea that which screen is the primary.
But wait..
If you are worried about whether you are following the standards (industry) or not then read below:
If all your activity has the equal importance for users (including the tabbed one) then recommended way is to use the fragments and on the other side if you don't want people to get distracted by navigation drawer icon or tabs then better to open a new activity with just a back button on top..
I hope it would give you idea how to proceed..

Is it possible (or advisable) to use Activity as switched component of ActionBar's navigation tab or drop-down navigation

I have 2 Activities. One of it is displaying line chart, another is displaying pie chart. I would like to embed both in a single launched Activity. User is allowed to perform single click, to switch between line chart and pie chart.
I realize ActionBar's navigation tab, or drop-down navigation is the best candidate to help me achieve this purpose.
From Android API demo, and guideline found from http://developer.android.com/guide/topics/ui/actionbar.html, I realize all switched components are implemented as Fragment, not Activity
Does it mean that I need to port my previous 2 Activities into 2 Fragments, in order to embed them into ActionBar's tab navigation view/ drop-down navigation view?
Is there any other ways I can do without porting? But, is it advisable as I do not find an official example by using Activity.
In API demo, I realize most Fragment is implemented in the following pattern.
public class FragmentStack extends Activity {
...
public static class CountingFragment extends Fragment {
// CountingFragment never access any members in FragmentStack
}
}
Is there any reason to do so? Why don't they have CountingFragment is a separated file?
If you want to embed in a single activity, the preferred way is to use fragments. You could probably also use custom views, but Fragments have a well defined lifecycle and handle a lot thigs for you. So it might be a good idea to extract functionality into fragments, and have the activities only as shells, embedding the fragments (if you still need this with your new design). You can switch between fragments any way you like: with buttons, dropdowns or any other UI that fits your app; you don't necessarily have to use the action bar. As for 3. there is nothing wrong with defining fragments in separate files. Activity and fragment are in the same file in samples mostly to make it easier to follow the sample.

Using tabs inside a Fragment (not FragmentActivity)

I'm currently creating an app in which the main screen is build up out of 2 Fragments.
When the user selects options on the main screen, one part of the screen gets replaced by a new Fragment, all pretty much basic stuff.
Now I'm trying to create a screen with several tabs, which all open a new fragment inside them. I had this working with regular intents, but that was before switching to Fragments.
I read that this is possible by using a FragmentActivity, but sadly you can't replace a Fragment with a FragmentActivity, simply because the transaction won't let you.
Is there any way of doing this inside an ordinary Fragment? Or should I try mimicking the behavior by using a layout with a fragment inside which gets replaced by another one at the press of a button, much like the main screen? (Or won't that work due to fragments in fragments?)
There is an example in Android's support library that describes what seems to be what you need. You can find it here: FragmentTabs.

Categories

Resources