This question already has answers here:
One Activity and all other Fragments [closed]
(8 answers)
Closed 8 years ago.
I just need a little guidance on how do I structure my android app
I have an android app that has five to seven screens. Now the way I have structured the app is that I have one single activity that loads on app start which is more like a gateway to start exploring the screens. This activity also has container which I use to show the fragments. Every fragment represents a screen.
So is this structure of one single activity and multiple fragments a valid and a good one or may be the app can be structured in a better way?
There might be various ways to organise an application,
You can organise it by component/class types
activity/
// all activities here
fragment/
// all fragments here
service/
receivers/
utils/
Or you can organise it by features you provide in your app(say by UI)
feature1/
ActivityOne.java
FragmentOne.java
FragmentTwo.java
FragmentThree.java
feature2/
ActivityOne.java
FragmentOne.java
FragmentTwo.java
FragmentThree.java
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 have an android application working in mobile, this app has two types of activities ones that loads a list of items, and forms that open when you touch one item.
Now I have to port the app to tablet and the layouts need to be fully restructured to fit big screens, so much that the java code has to be heavily changed so i thought to merge both activities in one as shown below
Is that possible?
And if its what i need to use, fragments?
Can each activity still in its own class?(this is critical)
Can each activity have its own network operations and AsyncTasks?
Yes as you said you can use fragments for your situation (Infact their main purpose is to support different screens without code duplication). So you'll have only 1 Activity class and 2 layouts for different devices and you just need to do some run time checks and perform actions according to them.
Here you can find tutorial :- https://developer.android.com/training/basics/fragments/index.html
is not possible because only one activity working in single screen in android. i have one idea to put two fragment in single activity then use your own AsyncTasks network operation.
This is a follow up question to this question:
Open another app within a fragment
I'm creating an application with a single activity with a SlidingTabsLayout and every tab is a fragment. Is there a way to open an installed application and just run it as it is inside one of the tabs? The other application is just a single activity which I want to display in my tab as it is. The answer in the question above said I can't do it, but maybe in the 1.5 years that passed since the question was asked, something has changed?
Is there a way to open an installed application and just run it as it is inside one of the tabs
No.
The answer is the question above said I can't do it
The answer is correct.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm building an android app which, although it has a completely different function, has a very similar UI to Instagram. There is a bottom bar with buttons for viewing timeline, posting (my app's content), checking notifications, and viewing your own profile.
I am a fourth year electrical engineering student, but I am relatively new to Android development. In order to teach myself more stuff faster, I went ahead and made a lot of the functions of my app (like viewing posts, profile, timeline, friends, etc.) their own activities with their own bottom bar (which are just buttons as of now). In doing this I have learned a lot about development and have built an app that, for what I have built out so far, functions as it's supposed to, but is not too fast or pretty. I realize this is probably not the best practice and I wanted to ask you all what you think a simple explanation of how to implement an Instagram-like UI would be? When I say Instagram-like I mainly mean: it includes a bottom bar; the app "preserves" where a user is, like if I'm viewing a friend from the timeline, then press the view profile button, then press the timeline again, then I will still be viewing my friend.
I have tried to read up as much as I could to tackle the redesign but I'm still a little fuzzy on the standard for doing something like this.Should I try TabHost (which is deprecated)? should each button represent an activity with swappable views (for example in IG I click a post on my timeline and it swaps to the pictures post view)? Should I associate an activity with each button and swap fragments based on where the user navigates? I realize this is an open ended question, and I hope it's not too vague for Stack Overflow, but I have thought about it a great deal, and I would appreciate the feedback of some more experienced developers! Thanks!
You can use the new TabLayout that is contained in the support design library along with a view pager to switch between your different fragments.
You can read about the TabLayout as well as some of the other things packed in to the design library here.
Reference for TabLayout
edit: In the comments I mentioned an analogy for you to think about how to use fragments vs. activities. In the interest of not spreading misinformation I'm gonna back peddle on what I said a little bit. You can absolutely use separate activities and switch between them using the tabs. So, the correct answer is kind of somewhere in the middle. The MainActivity would still hold the tablayout and manage the tab listener and switch between the tabs but you can set the content of the tablayout to hold the content a new activity. But you can do it with fragments as well and possibly more efficiently.
It really depends on the structure of your app and how deep it goes. If you only have 4 fragments and the information is centric to one work flow then you might choose to only use one activity with four fragments. It is really up to the developer on how to manage this.
Since fragments have come out, one philosophy has been if the space that you are using can be repurposed, that is, if you are working in the same box (the area above the tab bar of the instagram application for example), use a fragment. (Referencing from here). The camera tab is a good example of where to use an activity. The camera functionality represents an entirely different workflow than is seen when navigating feeds, pictures and profiles of the instagram application. So the feed might be one fragment, and the user profile another fragment but they are all being managed by the same activity and using the same content frame.
There are other answers posted in the reference I provided above which should help your understanding further.
This question already has answers here:
Swipe between activities
(2 answers)
Closed 9 years ago.
I'm pretty new to Android development, but have basic knowledge of Java (comming from Python). I'm working on a simple app that will list 10 WebViews side by side, but I'm having some trouble finding the proper way to build the layout and activities.
I have - for example - an arraylist with the uri's I want to build. From this I can build the same number of WebViews. How do I place them side by side (taking 100% of the screen) and allowing the user (me) to swipe between them? Should this be in a single Activity or should each swipe start a new activity?
EDIT: Sorry for the duplicate question.
You can implement them as ten separate WebViews, and then put them in a ViewPager. The ViewPager will automatically handle the swiping from side to side between the WebViews.