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.
Related
I have a use case and couldn't find an example of it online. So, my use case is I have a few fragments and the flow is decided by the server.Server returns a list of strings and based on the String, I have to navigate to a fragment. Eg:
Server returns ["detail_entry","accept_work","qr_code"] and depending on that I have to navigate using the navigation architecture component to DetailEntryFragment,AcceptWorkFragment or QRCodeScanningFragment. How to create Navigationaction directly in Kotlin code rather than use the navigation graph actions in xml.
I do not want to create an action between each and every fragment in xml and use it.I also want to mandate arguments passing. Is there any other way other of how to achieve this with the navigation architecture. I do not want to move to the old fragmentmanger and committing with them.
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.
I know it was answered before but I am still confused, if I should use fragments or activities for navigation drawer.
What is better practice? Google does not say anything about it and I am a little dissapointed.
Also, if I create a new project from Android studio with navigation drawer template, what I get is one single activity and one single fragment, but there is also separate fragment for navigation drawer and inside it, there is an interface and above it is a comment saying: "Callbacks interface that all activities using this fragment must implement.". It confuses me even more because I think I should use activities, but I am not sure.
I will keep it very simple: When you switch between activities , user has a bit of feeling as if we are taking him to another view(as if we are making him switch somewhere and the whole view is changed with a sudden blink) but when you do same stuff in one activity and changing views through fragment, it is very smooth. Moreover passing data from one fragment to other is very easy and less expensive as android says activity transactions are expensive.
Yet many times it depends on your requirement.
Keep in mind that a nav-drawer can be used for different user interactions. By the common usage as a navigation element, you will implement it by fragment(s). In this approach the drawer is placed on the left side.
An other approach can be to perform actions by pressing an drawer entry. In this situation you wont replace any fragments and only implement the drawer to the activities which should be able to perform this particular interaction (maybe: "send contact per a email")
In the android development tutorial on the navigation drawer design pattern the recommended way of managing the different layouts is using fragments. These fragments are swapped through the FragmentTransaction().replace() transaction. In my opinion the navigation drawer fragments are usually exchanged and reloaded repeatedly. Therefore wouldn't it make more sense to use the FragmentTransaction.show() and FragmentTransaction.hide() calls in this context?
What Best Practice for using the NavigationDrawer right now?
Yes they are loaded repeatedly, just like activities...
Best practice is what Google expose: FragmentTransaction.replace() because:
they know what they're doing ^^
it keeps only one fragment loaded at a time
If your main concern is about using fragments in Navigation Drawer .. then you can use it.
Fragments just provide a way to easily replace one layout with another without creating a new activity.
If you still want to use activity for your navigation drawer then you can use that.
Now as far as maintaining it's state is concerned then android actionBar in itself provides methods that can maintain your app state.
Now as per Android documentation as you are referring .. it is always good to have fragment as they a light weighted in context of to use new activity each time for your app.
Hope it helps!
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.