ActivityGroup VS View flipper - android

I have a group of tabs. In one of the tabs, I want to display two different types of functionality.
So my question is it better to implement a activity group with child activities or just create a view flipper and switch between views.

ActivityGroup is used to manage one or more activity example. if two different activity have different implementation but the user want to see on sharing screen basis means in first half user want to see some thing and in second have some other thing. In that case ActivityGroup comes to play
ViewFlipper can be used if you want to periodically change the views. Say like an automated flipping book of some sort. Though a custom-adapter gallery is much better at this.
Both have different purpose. It depends on your requirement you will choose any of them.

i would go with a view flipper. you probably dont need the whole overhead of creating another activity. but that way will make your single activity that will hold them a bit larger. so also maybe try to implement some of the functionality inside the views you will be adding.
hope this helps.

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

Multiple instances of an Activity - is this bad practice?

I am creating an app which will have multiple 'grids' containing a title and image thumbnails in each grid square.
Each Grid will have different content stored in it.
I have so far created one activity that initialises an instance of GridView, and uses a custom GridAdapter. (See photo for what it currently looks like) I was planning to swipe left to create a new empty grid in which the user can upload content. There may be anywhere up to 50 grids.
I'm just learning how to implement the gesture, and how to create a new instance of the activity, but from what I've read, I am thinking I have designed it badly.
I was planning for each grid to be an Activity (each takes up the full screen).
I envisaged an Activity as being like a Class in java that you can create instances from a blueprint. I thought if I created one 'Grid' I could create a new instance of it each time. Fragments didn't seem appropriate at the time, as the android tutorials often described them as being purposed to add components to activities.
I'm starting to think though that I am using the wrong methodology here and I need to change it? Can someone guide me in the right direction? I have written all the code already - if I need to change it, do Fragments and Activities share any methods, meaning I can retain some work?
Like you mentioned, using activities for holding content in your use case where switching may be triggered using gestures will definitely be resource heavy and cumbersome. Since, you mentioned swipe gestures, I believe fragments would be much lightweight in this situation. In fact, I would suggest you even look at ViewPager which even recycles fragments for you and optimizes user experience by loading the next fragment for a smoother experience. It will also handle swipe gestures for you!
[UPDATE]
Based on your updated explanation of the user flow, I'm certain that the ViewPager would fare as a better option mainly because it allows for a much better control and user navigation. It will also take care of handling swipe gestures and memory issues that come with these types of flows. Moreover, it will even allow for a page titles and bottom tab indicators in case you need them.
It will require each of its pages to be a fragment (your ViewPager will itself reside in an Activity). Once the user clicks on a grid cell, you can show a dialog window from where user input can be captured. This setup should be optimal for you resource wise in my opinion.

May two View subclasses work as two Fragment in one screen?

I have a little app wich contain MainActivity and two subclasses of View - FirstScreenView and SecondScreenView.
Now MainActivity manages both subclasses and output them successive.
I want to show both classes in one time in a one screen (in this layout - firstscreen.xml that is used instead of main.xml now. main.xml does not exist).
I know that this challenge can be overcome by applying the fragment. But my classes extends View, not Fragment.
May two View classes work as two Fragment in one screen?
Please, tell me in what way and how should I solve this problem.
Thank you (and thanks Google Translate).
Talking from a UI perspective, the app will look the same. Talking from a functional perspective, the app could do the same. The only difference is that if you use fragments, you will have to manage both lifecycles.
I rather preffer what you have done, working with these two views. Remember that if you work with custom views, it is a best practice to delegate its logic to the activity, something you will relay to the fragmeents in the case you use them.

Android - Fragments or custom views? Which should I be using?

I am trying to create a layout in an activity that will look different in landscape and portrait, however it will contain the same sections, just in different places on the screen.
Most examples I find on fragments is always the list and detail, which is not what I'm looking at.
In my example I have, amongst others,
a scrolling image section (carousel)
a page title with some brief details
a carousel of thumbnails
two buttons
full details of the page
Now in the landscape layout, the image section will always be on the left side with down the right side, the page title, carousel of thumbs, two buttons and full details.
In the Portrait the title will appear at top, with image carousel beneath, then thumbs, then buttons etc.
The way it works in my head, is that each section should be a fragment, and then depending on the layout file in the respective res/layout folder corresponding to land/port, the fragments are arranged accordingly. But I'm also thinking is each section a whole fragment? Or could it be a separate view that get's loaded in, but just in a different order depending on the screen orientation.
I hope that made sense?
Does anyone have any good tutorials that explain exactly when you should and shouldn't use fragments. All the usual suspects just list the list/detail example, which is not applicable in my case.
tl;dr Can I use fragments as modules/blocks in various layouts or should I just create other layouts and display them inside the main layouts.
Fragments can be used to have split screen and to store previous state unlike views. It is difficult to manage back button with views
If you just want to change how the screen is laid out, the answer is neither. You can define orientation-specific resources (either a separate layout file for each orientation or different dimensions/constraints for each orientation) and the system will just provide you with the correct resource set for the current orientation.
That said, it sounds like you may be describing a modified version of master/detail flow. If this is the case, fragments are a good way to go since the landscape view is actually several separate screens in the portrait version of the app, so each section needs it's own state and lifecycle, which fragments provide.
Now, I know you said you wanted an example beyond a list and detail view, so here's some more details on when to use fragments:
Fragment, like activities, have state and lifecycle. Custom views do not have lifecycle and are completely dependent on the activity or fragment containing them.
You might use a custom view when you have a widget on screen that is used in multiple places and is just like any other view – bound to the activity when the layout is inflated and controlled from there. It's a way to either reduce duplicating combinations of views in your layout or to draw a custom view that doesn't exist yet.
Fragments are good when you need some state or lifecycle for a section of the app that might get used in multiple places or shouldn't be logically connected to the activity it is contained in. If you use the new Navigation Component, you actually just define one activity and then each screen in the navigation tree is a fragment that gets swapped out as the user navigates around the app. Here each child component on the screen (each "screen" that the user navigates to) has it's own lifecycle, business logic, etc, so mixing the code for all of that in the activity wouldn't make sense.
So the question comes down to what you are trying to build, and this may be a case where the best way to learn the difference is to try each option out as bit. The differences become more clear with practice using them. As a general rule of thumb, personally, I only really use custom views when I am trying to make a new view that doesn't exist elsewhere. If what I'm trying to do is simply a matter of laying out existing views in a new way, the answer is probably some trick in the layout file or layout code inside the activity. If I'm trying to make a stand-alone piece of the app that does stuff, especially if it also appears in multiple places in the app, I'll probably be building a fragment.

Converting Multiple Activites into a Single Fragment

I've recently decided to update my app to support the new fragments feature in honeycomb 3.0.
My Application currently works on a list view that opens different activities depending on which list item is clicked.
Using an adaptation of the code in this tutorial I have created an app that consists of only two activities, but depending on which list item is clicked the second "viewer" activity launches using a different layout xml.
Unfortunately I haven't been able to figure out how to call the old methods that had all the functionality. Should I Import all of my old activities and then call the methods into the viewer activity (I may need some advice on how exactly to do this) or should I just put all the methods directly into the same viewer activity (please consider the size of these methods(which is very large by the way)).
Once everything is working with two activities upfront then it will be a pretty simple task of "fragmenting" the app as demonstrated here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
Thanks
As James has pointed out you will have to move the business logic from your Activities to your Fragments.
To handle events you can create a listener Interface. The CONTAINER activity/ies will implement this interface. As fragments has access to the container activity you will be able to delegate to the container Activity the "logic" for the desired events. For this events the activity will decide whether to launch a new activity, show/hide new fragments or whatever.
I had a similar question, take a look to the question and answer: here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
I think its possible to allow multiple fragments to occupy the same space in an activity. Again, take a look to the answer here ... I think the concept/scope of Activity has change a bit and now an Activity can contain different Fragments which every one will allow user to do a single focused thing.
I'm not sure what you mean by "call the old methods that had all the functionality". You'll want to rewrite all of your activity classes as fragments. Check out this tutorial here (it's very concise). Basically, you'll want an activity that consists of a ListFragment and a FrameLayout. Your ListFragment will update the FrameLayout by changing to the appropriate Fragment based on which row was selected.

Categories

Resources