I'm currently designing an android app and tried several kinds of general setups, each seeming to have it's downsides.
The app generally consists of three screens (A, B, C), each of those has a list. On list selection it should change to a detail view (A1, A2, A3).
I want each detail view to be swipeable back to it's list.
All I'm sure about is that I have three buttons in the action bar which let the user switch between A, B and C.
Beyond that I tried those setups:
The whole layout is one ViewPager with six fragments. Downside: I have to implement the logic "when and where to swiping is allowed" myself - seemed wrong to me
The layout consists of three fragments, each containing a Viewpager with two fragments. The Viewpager-Fragments are replaced on Actionbutton-Clicks
Downside: I have to forward all State changes to the inner fragments
The layout consists of one Viewpager that gets its Adapter replaced whenever an actionButton is clicked and thus always only has two Fragments at a time
Downside: Fragments are often destroyed and recreated
I hope I made my problem clear.
What general design would you recommend?
I would recommend having fragment with viewpager that would show three fragments with lists. Now, on list item select, I would replace this fragment with item details. When user want to swipe back, i would add custom gesture handling that would call onBack. I would argue if such gesture handling is good design but I'm guessing that you simply need to mimic IOS behaviour (which probably most of us have to do too damn often)
Related
I set up my application in the following way and am curious if it's considered "best practice." I have two activities and two fragments. Activity 1 launches and immediately uses Fragment 1 to display a RecyclerView of items. An Item is clicked in the Fragment, it's communicated back to the Activity through an interface, some logic occurs and Activity 2 is launched, which immediately uses Fragment 2 to display the detail of the selected item.
I did this because A)I like the logical flow of Activities within an application and 2) I needed to create tablet layouts in which I could use both the Fragments to fill the screen.
The more I'm looking at this thing, I'm thinking why not simply have 1 Activity that manages both of these Fragments? Activity 1 launches Fragment 1, item is clicked, info goes back to Activity, FragmentManager replaces Fragment 1 with Fragment 2.
My question does one of these ways adhere more to "best practices" or are they both fine and it's a matter of choice?
You've not described your problem clear enough to use more than one Activity, other than unrelated data to the list and what happens when you click there.
What you described is a "master-detail" flow, and that is a good use case for one Activity and two (or more) Fragments - a list + some detail page. This especially makes sense on larger screens when you can show those Fragments side-by-side.
For example, you can have an Activity that holds a navigation screen (whether that be tabs, a drawer, or a bottom view), then everything you navigate to within there is a Fragment.
Otherwise, you redirect to some "settings" page, for example, that is a new Activity, which demonstrates the "Single responsibility principle" in your UI.
Well you can go with the single activity - multiple fragments. You can pass data between fragments using bundle as well. Matter of choice also depends on the use case. But fragments are made to use as light weight activity that requires less resources then activity. Most of the things are possible with fragments. So unless it is not required to use activity my choice goes with single activity - multiple fragments.
I have an activity that has within it one fragment that takes up the whole screen. At some point in the app flow, the user can go to another screen in the same activity that is composed of two seperate fragments. So you can imagine it as:
Fragment A (100% of the screen) -> Fragment B (50%) + Fragment C (50%)
I can think of two ways of doing this and neither one of them is particularly good. The first is to set a layout for the activity that has in it one container that will hold Fragment A, and then have Fragment A open subfragments B and C inside it. I'm trying to avoid using subfragments because it leads to unusual lifecycle bugs and it also isn't supported by all version of the api.
The second way is to have two layouts for the activity - one layout having a single container, and the second one having two containers and then switch between them at the appropriate moment with setcontentview. I have to admit that I'm not too happy about that solution either, since it means the user will see the screen redraw white instead of a nice transition effect.
Does anyone have any suggestions on how to do this most efficiently? Note that I do want everything to remain under one activity - logically it should be this way. There's no logical point in having two seperate activities for this UI movement.
solved by having two containers and setting the top one to wrap_content height, visibility=invisible and not populating it at all. When I need to move to the two pane setup I populate the invisibile container and set it's visibility to visible which causes it to remeasure. When moving back from the two pane to the single pane call remove on the fragment that populates the top pane.
I have an app in which I have three tabs on the ActionBar and and each tab has a Fragment associated with it in the ViewPager such that the user of the app can scroll between the different tabs/fragmens using a swipe gesture. So there are 3 tabs and 3 fragments and there is a one to one relation between them.
What I want to achieve is that inside each tab there should be 3 fragments and user should be able to navigate between them using the swipe gesture. Similarly other tabs can also contain different number of fragments and user should be able to navigate between them. So in this case there are three tabs and those can have "n" number of fragment such that there is one to many relationship.
How can I achieve such a behavior using or do I have to change my current approach and do something else. Kindly provide me a path here, I am stuck on this for quite a sometime.
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.
I'm currently developing an application that has a tab bar, and 3
different views: the first is a master-detail, the second one a
gallery, the third is a simple webview.
I was using the TabActivity, but since Android 3.0, this class is
deprecated and Android reference
suggests to use Fragments.
I switched then to an ActionBar, with Tabs and Action
Items. Inseide the first tab item I have a layout with 2 fragments (my
master-detail view). When I switch through tabs I want that my layout
change as I described above, so I thought to hide the left fragment
(the master listview) and work only in the detail fragment.. but with
this solution I have only one main activity with a lot of fragments
attached to it, and for each fragment displayed I need to modify the
Action Item shown and handle different actions in
OnOptionItemSelected.
Is this a good way to implement this kind of
application or should I consider different solutions?
You should have a single fragment container where the fragments are replaced depending on the tab selected.
One activity and multiple fragments is the right approach.