I am starting to develop my app and I implemented a NavigationDrawer and I've got a simple question. Should I use Fragments or Activities to display each "content". I mean, I read that Fragments are better, but in this case I would only have like one Activity for almost every part o my app...
What do you think? I should decide now which approach I'm going to use.
Related
I am learning android from two resources one from video and one from book +(android developer resources ). Both show how to have multiple views in a different ways. Video one used activity and intent book used fragments. What is the best practice fragment/activity?When to use Activity and when to use Fragment.
You will most likely hear things like
You can have an Activity without a Fragment but you cannot have a Fragment without Activity
You can have multiple Fragments under a single Activity
You can use Fragments to have multiple sections in a single screen
Fragments has its own lifecycle
So I'd give my personal thought and a little bit of scenario based on my own experience on how I would decide which to use based on a given scenario.
Consider a Registration Form where I'm required to create 3 screen features of which contains Basic Personal Info, Financial Information and Online Mailing Information, in this case I would use multiple Fragments under 1 hosting Activity named RegistrationActivity.
Now consider same application where I'm required to create a User Setting Screen it wouldn't make sense if I create another Fragment under that same Activity right?, so that would be a separate context and everything about it will go to another Activity named SettingsActivity
There are more technical things under the hood, but most of the time you will decide which to use based on the use-case you are dealing with.
There is also an up to date architecture namely Modular design where each closely related components are grouped inside a module library, majority of such project will fall under the feature category where most of the time if not all, have single Activity hosting multiple fragments. And going back to my scenario it would be like a Registration Module
Hope it make sense.
Have a look at another similar post
Why fragments, and when to use fragments instead of activities?
I want to know if I am properly using Fragments here...
My application contains 6 Activities. I wanted to add a Navigation Drawer to it so I am currently in the process of converting each of these Activities to Fragments. When I select an option from the Drawer, the appropriate Fragment will appear.
My Fragments are very "stand alone" meaning each Fragment is self contained and not interacting with other Fragments, for example a calendar that displays the date.
This would look the same on every device, whether a tablet or phone. EVERY tutorial I have seen about Fragments has multiple Fragments interacting with one another. Mine does not. My question is, from a design perspective, is it ok to use Fragments in this manner? Or should I just use Activities instead? Thank You.
If it is from the drawer, the proper design is to use fragments. You should be using Fragments whenever it's possible, as it makes your code easier to maintain and control. Communication between fragments are easier than communicating between activities.
In addition, activities are expensive to operate. Activities created also do not implicitly destroy previous activities.
I'm planing a new app with a navigation drawer.
It is better to use one activity which manage many fragments or to use many activities with many fragments? Are there any serious advantages or disadvantages?
I read many articles about this but there are from 2012 and older.
(I'm just planing to read and insert some datas into/from the database..)
From my experience I would recommend rather having multiple activities with many fragments. If you use a single activity you are going to find it harder and harder to manage fragments with the activity lifecycle.
For example if the activity is destroyed (e.g. if phone is low on memory and user receives a phone call or you call an intent to open the camera), when the intent is recreated you will need to handle recreating the fragments and their states.
With a single activity this can quickly become a nightmare to manage if not done carefully. With multiple activities it's easier to manage and helps seperate portions of the app - making it easier to debug as well.
An example of how something simple can become complex with a single activity would be something such as the back button.
If you need to handle it different for different fragments that means your activity is going to need to cater for which fragment is currently visible, as the activity overrides the backbutton, not the fragment. This also would likely mean you need to add interfaces to notify the fragments of a back button press.
Stating all of this however, there are some apps that benefit from a single activity. For example if you have a viewpager for swiping fragments (e.g. pages of a book) or fragments that do little interaction, then a single activity can be beneficial.
I was able to get a TabActivity working properly with multiple Activity's using ActivityGroup. However, I noticed a weird behavior. When clicking on multiple screens in one tab, going to another tab and navigating back to the original tab where user was already navigating several screens, the ActivityGroup goes back to the main Activity rather than the last visited one.
I was following the tutorial here:
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
Is there a work around for the ActivityGroup to prevent this kind of behavior?
I think it is better to look for a tutorial on Fragments.
I know this is not really the answer to your question, but I think it would be better to immediately use Fragments. This because Fragments are now recommended for Android development, and should be used as much as possible in favor of older technologies. And the stuff you want to do, are the perfect example of things that should now be done with Fragments.
The tutorial you are pointing to is from 2010, so that means that the technology used there will be rather old (I haven't read it though, but I can imagine it will). It is best if you want to develop something, to immediately use the latest technology, in this case: Fragments.
Some information on Fragments
I understand that android is a completely different thing than browser programming.
But coming from a web development background, I wanted to undertand what fragments are supposed to be used for ?
Are they similar to the concept of iframes in an HTML page having its own UI and can be added to any other page?
If not then what is tje general use-case of android fragments ?
They are reusable parts of your applications. You can have for example two different Activities using same Fragment. You may want to read Fragments Design Philosophy in the android documentation.
Here's also short description from the same documentation page:
A Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
Yes, I think you can think of them as the same at a conceptual level.