Managing fragments in Android - java

Hello i am fairly new to android development and have been using Activities to navigate through my app. Now that i have gotten my feet wet i'd like to write more efficient applications. From what i have read, fragments are a way to limit the creation of unnecessary Activities and reuse UI elements.
I am trying reduce the amount of activities in my applications by converting them to fragments. When a button is clicked, do i call the next fragment from the current fragment, is this good or bad practice? What is the role of the main activity if fragments are directing the flow of the application?

Fragment is good practice but you need more careful handling fragment because it's tight coupling is when a group of classes are highly dependent on one another.
read this tutorial it will help to understand fragment flow [https://guides.codepath.com/android/creating-and-using-fragments]
Fragment is view and Activity is parent View (MainController is must be actvity)

Related

How to change layout on button click in android app?

I am new in Android so correct me if i am wrong. When another activity is opened, the first one is destroyed. So i have to pass all variables to the new activty if i don't want to lose the data. Or, can I run another not UI thread that manages my data?
Basically I need to change layout in my app on button click. Is there any way to do it within the same activity or i have to start another activity with the new layout?
You can use one activity in your app and do what you want in fragments. Navigation is good tool to use for relating fragments to each other.
well initially you can use a single activity and in that activity you can call multiple fragments as per your need,
and for the data you can use MVVM architecture with room architecture, it saves your lots of time and code.
and go with navigation graph if you want a quick and easy implementation.
you can start all this with this demo.

multiple fragments - where to commit them?

I'm trying to wrap my head around how and where i should be setting up my fragments.
Use case senario i'm trying to implement
I have a mainActivity that has a bottomNigationView widget that will open different fragments (fragment A, B and C)
In FragmentB i ask the user to input some information, then they click the next button which should should load another fragment lets say called FragmentB2
FragmentB2 should carry over some information that the user imputed from FragmentB
My question is, should i be making the fragment transactions of both fragments B and B2 in the mainActivity? Since i read online that it's not good practice to have nested fragments.
Currently, what i have is inside of FragmentB, i start a fragment transactions when the next button is clicked, so that it creates and goes to FragmentB2. I think this is whats called a nested Fragment, correct?
If you're near the beginning of your project, this is a great time to look into using a navigation controller from google's new architecture components.
https://developer.android.com/topic/libraries/architecture/navigation/
This will let you abstract the management of these individual stacks of fragments. I know this is a bit of a tangential response, but if you are worried about nesting, maybe take a step back and see if you can put together the higher level scaffolding of your navigation first.

Do fragments keep running even if not on screen?

I have a Android app that shows lots of real-time data jammed onto one large scrolling activity.
Now i want to split it up into two simpler screens using fragments, where only one fragment may be on the screen at any one time.
I read up a whole lot on fragments and watched several videos, but before i start ripping up my code to convert it to fragments i wanted to know the following.
If i create two fragments A and B, then while showing fragment B, data comes in for fragment A. Can the controlling activity still communicate with fragment A giving it data even though its off screen? OR do i have to save the data somewhere and then when the user switches to fragment A then I give fragment A the data to be shown, while saving incoming data for fragment B which will now be off screen?
The problem is that right now im not saving any data because everything is on one screen, so as data come in i just displayed it, but if i switch to using fragments i dont know if i can do the same thing by passing the data to the fragments even if they are not on screen at the same time.
Thanks.
If you retrieve your data with multiple asynchronous requests in your Activity, you may create a fragment for each of them and move related retrieval operation into that fragment (probably to oncreateView() method). Then, you can use ViewPager (probably with TabLayout) in the parent Activity to be able to use all those fragments. Therefore, your Activity only deals with setting the ViewPager and leave the rest to fragments.
ViewPager shows one page at a time but it can initialize other fragments as well, even before they are shown. You can use ViewPager's setOffscreenPageLimit() method to increase that range.
In case you need a communication channel between fragments and the activity, you may create callback mechanisms, as described here.

Drawer, Fragment and Activities

I have some questions about designing applications with NavigationDrawer.
I create an application with NavigationDrawer composed of one Activity and some Fragments. For example I have four Fragments, and every Fragment has some actions.
Which is the correct way to implement that?
Adding all actions to my Activity?
Create some other classes and call them from MainActivity with context/view?
Other?
Any information about these questions is really appreciated. I also need some example or resource explaining which is the correct way to implement this.
Anyone can help?
Check the google IO 2014 source code to get a good idea of the nav drawer implementation
https://github.com/google/iosched
All the navigation and fragment transactions should be done via the Main Activity or the activity holding the fragments. Any kind of fragment - fragment communication should be avoided. The developer docs shows a good example of how to handle fragment communication via Activity using interfaces
http://developer.android.com/training/basics/fragments/communicating.html

Eclipse ADT automatically creates a fragment for Activities. Why?

I vaguely understand what fragments are used for. However, I don't understand why ADT seems to suggest that we should ALWAYS use them. I am aware that I can simply delete the fragment layout, as well as the code in Activity for the fragment. But is it ACTUALLY recommended to use fragments all the time? What are the benefits of not deleting it?
Is there even a point of an Activity having a single fragment? What is the difference with it having no fragments at all?
Additionally, how do you know whether to create multiple fragments in one Activity, multiple Activities with a single or zero fragments or a combination of Activities and fragments?
For example, if you have a Terms and Conditions button in your Activity, and you want it to open a screen containing a document listing the terms and conditions, should I be starting a new Activity for that? Or should I just move a fragment containing this content to the front?
Using Fragments is how modern apps are developed today.
Some of the benefits are a better performance when switching content (lets say 2 fragments)
because you are not leaving the current Activity.
Also, apps with swipe tabs (for example) are built with an Activity as a container and Fragments as the content itself.
Other benefit is that you can do much more with one activity, distributing work between the fragments. Each Fragment have its own Layout and therefore its own components, so maintaining the code and keeping organized is easier.
Eclipse does this way as suggesting a start point. You can totally ignore this and start from scratch. And that's all.
I suggest you to keep the Fragment to start getting used to it. Even if there is just one.
And, if later you need to work more with that Activity, it will be easier to add new features by creating a new Fragment, without modifying your previous code.
Good luck.

Categories