Clarification on the structure of my app - android

Some background info first. I require my app to show a feed of posts, implemented as cards. The data within the posts/cards is retrieved from my site's REST API. My app has 3 different tabs, each containing a different feed of posts/cards (but still the same layout).
Currently, I have an activity_main and the 3 fragments made for each tab (fragment_1, fragment_2, fragment_3). To my understanding, a Fragment is a layout that can be re-used on different Activities. Is this correct?
Additionally, where should I implement the layout/design for the cards? Should it be directly within the separate Fragments (even though the layout of the cards will be the same across all 3 tabs)?
After implementing the card layout, how would I populate the cards given the requested REST API json data? In other words, how would I loop through each result and insert the data into the card and display it on the screen?
Sorry for the noob questions.

Currently, I have an activity_main and the 3 fragments made for each
tab (fragment_1, fragment_2, fragment_3). To my understanding, a
Fragment is a layout that can be re-used on different Activities. Is
this correct?
Yes.
Additionally, where should I implement the layout/design for the
cards? Should it be directly within the separate Fragments (even
though the layout of the cards will be the same across all 3 tabs)?
Have a single fragment. Create 3 instances of this fragment and inflate the same layout/design if the layout of the cards will be the same across all 3 tabs.
After implementing the card layout, how would I populate the cards
given the requested REST API json data? In other words, how would I
loop through each result and insert the data into the card and display
it on the screen?
Pass a unique identifier that identifies the feeds (for this fragment) to the fragment as a bundle when you create a new instance and set them as the fragments argument. You can then retrieve this information to retrieve data relevant for this fragment.
To display the data, use a ListView/RecyclerView along with a suitable adapter to connect your data to your view.
I hope this answers your question.

OK. Lets try this. Yes, you are right, Fragments are basically layouts that can be used in different Activities. As it says on Android Developers site:
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.
Having that said, since it is the same layout for all three tabs, you can use only one Fragment and populate it with different data. So if you want to change anything, you would do it only within that one fragment.
For the cards I am guessing that you will be using a custom ListView adapter. And that you should (must) implement outside the Fragments.
And regarding the last question, there are many libraries that make consuming ReST webservices much easier. For example Retrofit.

Related

Stacking up android widgets/views in one layout file?

Is it advisable to stack up multiple views and depending on the preceding activity, show the appropriate widgets/views to user? e.g. To have 2 or 3 forms in 1 layout file(xml),and show one view depending on what the user wants to see.
you can use Fragments... Add the right fragment to activity based on the preceding activity.
Fragments do this. Stacked views are nice for collapsed elements. There are uses for them as well. The choice is based on your particular use case. From what you've said, I would recommend Fragments.
The nice thing about fragments is that they handle life cycle events (like activities) but without the need for serializing and parceling data to communicate among your fragments.
As for your question "Is it advisable to layer views?" I would advise against layering different forms UI. The xml design and maintenance can grow hair real quick.
Fragment is the solution in android when you want to show some specific layout to the user create 2,3 fragments android depending on the condition load the appropriate fragment.
You can go through this to get started on fragments.
https://developer.android.com/guide/components/fragments.html

Reuse ListFragment and detail fragment or multiple fragments

I'm working on an app that has like ten or more listviews across different activities, each of them uses a different adapter with a different data model and item row layout.
When you click on a row, the app shows a fragment with the details, nothing new.
My question is about the best approach to do this.
I could create ten fragments with a listview and ten fragments with a
details layout.
Or I could always use the same fragment and use a
different adapter and inflate a different details layout, depending
on a TAG, for example.
Another way for the details fragment could be
containing all the layouts in one, showing only the one I want and hiding the rest.
I also could implement a base fragment and extend it changing the layout in the children.
So my question is not about coding this, is about the cleanest and efficient way to do it.
Is it better to reuse one fragment or have multiple files?
Is it better to have a lot of files with a little code or a reusable file with a lot of code?
Thanks

Choosing between activity and fragments (showing map)

I have three screens looks exactly same with different set of data loaded on them.
What design pattern shall i use for it.
Tab1,2,3 also loads different data (Loading from database).
One activity and different fragments loading data accordingly.
Three activities using same layout.
Plane simple three activities. (Or more for tabs too - not sure)
I'm not really sure what your data is, but is there a reason you can't just cache the data in say, a content provider? Then you could use one activity, one fragment and just have listeners to load different data from the provider.

should i use a Fragment or an Activity for an ever present View in my Android App?

im developing an application with multiple screens using activities and fragments. The principle behind my decisions to use one or the other are based on the interaction in the application.
I have come to the conclusion that you should use an activity whenever major UI elements remain present after an event has occurred that forces the views inside the UI to change (An example of this can be a tabhost nested in the toolbar styled as the Google Play store android App with multiple fragments as childs. Another example is fragments representing the different rows or clickable elements on a navigation drawer).
So right now im presented with the dilemma of choosing once again between the two (fragment or activity) for a UI element that is going to be present across my whole application. Its triggered by a floating action button that launches a creation tool for an element inside my application and i needed to be accesible across all of my apps screens.
So to sum things up, what i need to know if its better to use a fragment or an activity for an element that is ever present across my whole application.
Thanks in advance
Use Fragments or a compound Views. But both in the right way.
Fragments are thought for reusable combinations of views. Compound Views are Layouts containing views. They are the right way if you want to create views from more primitive views, for example a View containing TextView and a increment- and decrement-button.
Fragments are more Activity-like. They have a real lifecycle. Your description looks like you want to create acitivity-parts. And thats exactly what fragments do.
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.
Taken from Android SDK Documentation: Fragments.

Use View or Fragment in ViewPager

I have a question about whether to use View or Fragment with ViewPager.
Background:
I have an Activity A that contains a ListView. Each ListView item opens Activity B. Activity B shows different content depending on which ListView item is tapped in Activity A.
Activity B's content is shown inside a ListView.
Question:
Now, instead of going back and forth between Activity A and B to switch contents, I have a requirement to implement horizontal view swiping to switch contents all within Activity B.
One solution I found (tried it and it works) is to create many instances of Activity B's ListView and use it with ViewPager + PagerAdapter.
Another potential solution found on the doc (haven't tried it) is to bring that ListView into a Fragment, create many instances of the fragment and use it with ViewPager + FragmentPagerAdapter or FragmentStatePagerAdapter.
My question is, what's the benefit of using each approach? Should I go through all the trouble of bringing the ListView into Fragment or just simply use ListView with ViewPager?
Thanks
A Fragment is a useful approach, I think, when you want to tie some UI business logic to a particular View (or group of). As you know, that individual Fragment has its own lifecycle callbacks and so forth, just as an Activity would.
Rather than having a single Activity host many ListViews through a single PagerAdapter, it may be cleaner to use the Fragment approach because the Fragment only needs to deal with the logic behind driving a single ListView.
This is a very similar situation to one I've just been facing. I'm showing various vertically scrolling forms (consisting of lots of input fields) within a ViewPager. In my case I have gone for the Fragment approach because in my case, it's possible that the ViewPager will actually need to display a completely different kind of view on certain pages. For example, on the first few pages, user input forms might be displayed. But on the final page, a graph will be displayed. A whole separate set of logic is required to drive that graph. To drive those input forms and one graph from a single Activity would get a bit messy, and I would probably need to contain the business logic in several delegate classes or something. So for me, Fragments were the obvious choice in the end. I have my InputFormFragment and a GraphFragment, and they each contain only the applicable logic for the Views that they supply.
Another thing to consider is that in the near future you too may want to display a different kind of View in your ViewPager. Or, you might want to have another UI layout altogether, perhaps one that doesn't use the ViewPager but displays them all side-to-side (e.g. a layout used on a large tablet in landscape mode). With Fragments, things are just far more modular and you could factor the code to do this quicker. If on the other hand you achieved your objective by using a single Activity that contains a simple PagerAdapter and all the logic for the ListViews within, you might find it takes more work in the future to support new kinds of Views or special tablet layouts.
One thing I will say is having implemented Fragments in a ViewPager myself through FragmentPagerAdapter and FragmentStatePagerAdapter, things can get a bit awkward if you have any special requirements; managing Fragments can be tricky sometimes. For example, for my UI I needed to be able to programmatically add and remove the ViewPager containing the Fragments. I also needed to ensure that the adapter in use didn't destroy Fragments once they had been shown, because I needed to collect data from all Fragments simultaneously at a certain point. Furthermore, I had to extend and modify FragmentPagerAdatper to make sure that the Fragments go through their onDestroy() properly and are removed from the FragmentManager when the ViewPager was removed.
Fragments enable a very modular way of constructing UIs for various screen sizes and orientations, and are excellent in how they allow you to encapsulate business logic and lifecycles for individual UI elements. However if your scenario really is just as simple as several ListViews in a ViewPager and you know that you will never need the modularity, then the overhead of Fragments could be an overkill.

Categories

Resources