I've been working on a app for the past month which basically consists of a TabControlActivity with a few ActivityGroups within this Tab.
I decided on using ActivityGroups basically because my knowledge as far as Android programming goes is very limited and at the time I had no clue that you're "supposed" to use Fragments for these things(Making sure that the tabwidget persists even if a new activity is started)
Now, Using ActivityGroups has forced me into doing things I really didn't think was needed. These are the methods I feel like ive been forced to implement in each ActivityGroup to make the application act the way I wanted it to.
I hope I've been able to pain a pretty good picture of my project. What I want to know is how much "effort" and recoding it would take to switch into using fragments instead of ActivityGroups.
Your question is not narrowed down enough. But from whatever I understood, this link might help. It says move the code from activity's callback methods to corresponding Fragment's callback methods. Thats a good way to start.
Good luck.
Related
My doubt is why we choosing fragment over activity even i too read that it is reusable i has its own life cycle etc etc. But as a beginner its quite difficult to work on it . Then i Have an another doubt to so far what i did in my project is i have only two activity one is for login and another one is for navigation drawer and action bar rest of things are all fragment which comes under activity 2 is it right way to do with or am making mistake . Many fragments are coming under activity 2 like multiple fragment inside a fragment is it right way it may be dumb question as a beginner is quiet difficult to understand for me can anyone clarify my doubt
Fragments were invented for tablets in order to combine several fragments on one screen. Most commonly they are used in master detail relationships. Look at this question from this point of view. Does it make sense to put two fragments on one screen? Are they connected?
Read this article one more time to clarify when fragments are needed. http://developer.android.com/guide/components/fragments.html
1) You don't have to use fragments if you don't feel they are necessary for your application.
2) Having multiple activities is fine. Most applications do.
3) Having fragments inside fragments is permissible, but you should just be aware that it can cause odd behavior in your application if you are not familiar with the lifecycle events.
4) In the Android Dev Summit (2015), a great overview of a sample Android architecture was presented, I encourage you to view it: here
Hope this helps to clarify things some.
I have read the documentation and some other questions' threads about this topic and I don't really feel convinced; I don't see clearly the limits of use of this technique.
Fragments are now seen as a Best Practice; every Activity should be basically a support for one or more Fragments and not call a layout directly.
Fragments are created in order to:
allow the Activity to use many fragments, to change between them, to reuse these units... ==> the Fragment is totally dependent to the Context of an activity , so if I need something generic that I can reuse and handle in many Activities, I can create my own custom layouts or Views ... I will not care about this additional Complexity Developing Layer that fragments would add.
a better handling to different resolution ==> OK for tablets/phones in case of long process that we can show two (or more) fragments in the same Activity in Tablets, and one by one in phones. But why would I use fragments always ?
handling callbacks to navigate between Fragments (i.e: if the user is Logged-in I show a fragment else I show another fragment). ===> Just try to see how many bugs facebook SDK Log-in have because of this, to understand that it is really (?) ...
considering that an Android Application is based on Activities... Adding another life cycles in the Activity would be better to design an Application... I mean the modules, the scenarios, the data management and the connectivity would be better designed, in that way. ===> This is an answer of someone who's used to see the Android SDK and Android Framework with a Fragments vision. I don't think it's wrong, but I am not sure it will give good results... And it is really abstract...
====> Why would I complicate my life, coding more, in using them always? else, why is it a best practice if it's just a tool for some cases? what are these cases?
I am sorry if I wrote too much, and thanks for your time. I hope I will get your attention, because I really need ideas and experiences about this topic.
Best regards, Ahmed
You shouldn't always use fragments. Fragments have their uses, such as when you want to page in and out parts of the screen or when you want to drastically change the UI in different orientations. When they make sense, use them. When they don't, skip them. I find they make sense in maybe about 10-20% of apps- I rarely see the need.
If there's a certain positive aspect apart from the simpler reuse of logic through different layouts, it's the ability of Fragments to be kept alive by the system at orientation change, aka while an Activity is reconstructed from zero, a Fragment can retain its instance, and therefore using them is more stable than an Activity. Also, switching between Fragments is quicker.
Personally, if I don't need to mess around with different orientations and layout sizes, I still prefer using Fragments and a singular container Activity around it, for stability and seamless switching between the different screens.
Its quite a general question and not directly related to a specific programming problem. But in my opinion good software is based on good design and therefore a good understanding and best practices. So your question is a good one for stackoverflow.
So, what about fragments. It took me a while to understand why you could or even should use them. As #pskink said, you can easily live without them. But if you are planning to rollout your software on different devices, you should definately think about fragments.
The screen resolution and density is not the only problem. Think about a smartphone. The screen is much smaller, so you can not present your app the same way as you can on a tablet. For instance a master detail flow. Left side, a list of elements and when you click one element, you will see details of that element on the right side. Easy to do on a tablet. But on a smartphone you would put the master-view into one fragment and the detail-view into another one.
You got two options to realize that scenario. Either programm different activities for smartphone and tablet but because they are actually doing the same logic, it's better practice to put the logic into fragments and reuse those fragments in two layouts (phone/tablet).
I have a time recording app which consists of two activities. One for managing the projects and one for time record manipulation.
The core of both activities are views I draw on my own. TimeRecord activity has a self-made calendar and Project-Activity has a TreeView.
Yesterday I started to play with the DrawerLayout. Everything works fine but I'm a bit unconfident because I had to merge all my code that was split in two activities into one.
I guess it's the easiest way to post some screenshots so that you can get a clear understanding of my app and I don't have to write many more paragraphs that describe how my app looks like :P
First the timerecord activity.
Second the open drawer with the former projects activity.
I already tested it on my devices (even older ones) and performance seems still to be ok. I just feel a little unconfident because now my activity has grown up enormously (same thing for layout-xml file).
Can anyone share his experience with me.
Cheers,
Stefan
You could split your Activity into two Fragments, one fragment with Tree and second fragment with calendar. This would be better option and you will be able to reuse fragments later.
I'm wanting to extend both a MapActivity and a FragmentActivity. I know Java doesn't allow multiple inheritance, so how do I do this? I've read something about a 'composite' type, but I've never implemented one so I don't know how to go about doing that.
Someone else HAS to have run into this before, how did you solve it?
EDIT: The reason I want this is because I have 3 activities in tabs; a map, an image gallery, and a settings list view. The code for all three of these "acitivities" is inside one big MapActivity called "Main". Yes I know this is ugly, and not good programming practice, and I don't remember why I wrote it this way. I think it was because I was reading most people recommended NOT having separate activities for separate tabs... which if I decided to split them into separate activities, I wouldn't have this problem anymore.
I have no idea why you would want such a thing, but I suppose the easiest solution is something like ActivityGroup, something similar to how TabbedActivity works.
I ended up solving this by going in and modifying the source of the Android support package and deploying my app with that. Outlined here, all I did was make FragmentActivity (android.support.v4.app) extend MapActivity. I don't think the reverse works... though maybe, and it would require you having the source of the mapping library you're using (in the case of Google Maps - no go)
I'm working on an Android app having an always visible tabbar.
However, each tab potentially contains many nested "screens".
Of course the back-button needs to handle this correctly.
I've now spent most of the day finding out what's the best architecture to achieve this.
There are also several similar questions on stackoverflow, but I couldn't really find an answer working for me. Two proposals I found and tried out:
switch view, see here
work with ActivityGroup, see here
Another approach I thought about is just implement all "screens" as normal activities and have them all have their own tabbar (but looking the same, so for the user it doesn't change).
I've seen that should be possible without too much redundant code by using include statement in layout xml and maybe create a common base class "CustomActivity" which configures the tabbar.
However since I'm not yet experienced with Android, I wanted to ask here before spending more time with try and error style.
Is this an approach which makes sense? If not, what would be a better solution?
Btw: The proposals mentioned above didn't work for me mainly because with neither the back button worked for me.
Thanks for every input!
Use Fragments API, ActivityGroup seems to be deprecated.