Should this be 2 fragments or 2 activities? - java

I have a tabbed interface for my program - there are two tabs: take photo and view photos.
As the name suggests, the user can take a photo in "take photo" and the user can view photos taken in "view photos". Right now the way its set up I use one single MainActivity and I have TakePhotoFragment and ViewPhotoFragment -- question is: does this contradict the principles in which Fragments are supposed to be used in? I don't really anticipate having both fragments displayed in a single screen (e.g. on a tablet), but I don't see how I can use one activity for each because of the limitations of the tabbed interface (when I created the activity in eclipse, I was prompted to select what kind of layout, I chose tabbed layout, and automatically code for fragments within an activity corresponding to several tabs was generated)
Can anyone help? Should "take photo" and "view photos" be fragments or activities?

It should definitely be fragments.
This does in no way contradict anything, plus I do not understand your concern about showing both fragments in a single screen. If you do not want that to happen, you just program accordingly. That is certainly not something that just happens because of the choices that you have mentioned so far.

Fragments is the best method you can use for the purpose you mentioned above. You can check the below links to know about the usage of fragments.
Creating a fragment
Fragments
android fragments
android fragments tutorial

Related

How to structure a particular activity (Android)

I am developing an activity that contains a kind of mix between a menu and a progress bar. Below this, there are views that are "visible" or "gone" depending on the progress in which the user is. I have programmed everything in the same activity, but I have doubts that this is efficient because we are talking about more than 1200 lines of code and most of it is only used in part of the progress.
Do you think creating a structure with fragments would be better? If so, how would you go about changing from one fragment to the next from a button within the fragment?

For supporting different screen size why should i use fragment in android instead of activity

I am beginner to android..I am started new android project..for supporting
different screen size..in fragment documentation they given to use fragment..but
why cant i use activity in android..if i use activity or fragment..which i should i use in this both..please dont give link of activity or fragment..please anyone answer me..i dont know which to use?...i want about all documentation they given about activity and fragment but i dint understand which to use..below is the link i read about fragment..if i use activity i should do more codings?
https://developer.android.com/guide/components/fragments.html
In fact you can't use a Fragment alone, Fragments are inside the Activity.
One point of using the Fragments for supporting different screen sizes is the ability to implement some views like a "Master/Detail" view.
A Fragment, as its name says, is a part of a bigger controller "the Activity", its reference can be removed and it's cleaner than having a big massive Activity to handle all the states of a view.
So the use case is completely depends on your project and its User Interface. I'd be glad to help you if you give me more information about your project and its design.
I think you will need at least one activity. And then for better handling different device rotations and screen sizes you can use one or more fragments inside this activity.
I try to explain this with an example:
You want to create a nice music player app which should look nice in portrait and landscape mode.
You split your app up into three fragments:
Here you can see how the app looks in portrait mode. The activity shows two fragments: The first fragment only consists of a listview. There the song titles are listed. On the bottom you can see the second fragment, which displays the song title of the current playing song and got a button for pausing the music.
When your user uses the music player app on a tablet in landscape mode you have more space for displaying stuff. Then the activity shows the list fragment (which also gets displayed in portrait mode) and it shows a third fragment which shows detailed information about the current playing song (e.g. the album image) and a progress bar.
By using fragments you only need to write the code for the list once.
Sweet and Simple thing, What i recommend is always use Fragments,
But for Fragment you will require Activity.
Take it in this way , Activity is a Canvas on which you can put any number of Fragments.
Whatever your UI is always use Fragment present on a activity if you want to show one screen even then also, So that you will always have Flexibilty to use all those cool things which fragments provides,maybe in future or in current.
If you use activity it has limits,FOR EXAMPLE, LIKE in INSTAGRAM AT BOTTOM, It has FIVE OPTIONS, Suppose THOSE OPTIONS ARE ON A ACTIVITY AND BY CLICKING ON THEM YOU CAN SWITCH TO DIFFERENT FRAGMENTS.
For more info:
Here is the most accepted answer for this topic.

What is the best practice for arranging Android apps with multi-screens and a fixed custom navigation bar?

I am making an app and want to make sure I am following good practices before I proceed further and potentially turn my app into a "big ball of mud" implementation.
So right now the general idea I have in my head is where you have a row of icons along the top representing the different pages you can click on. You click the button/icon and it takes you to that page.
So this icon-row along the top would be constant throughout most of the app. The only thing that would change would be the contents below that icon-row.
Is it considered acceptable practice to use fragments here? Use one main activity that has the icon-row at the top and then have the container below that "swap out" fragments based on the icon clicked? And then each page is really just a big fragment?
Does this make sense, am I following good practice? Is there a better way to do this?
I am making an app and want to make sure I am following good practices before I proceed further and potentially turn my app into a "big ball of mud" implementation.
If that happens, try a good brand of laundry detergent, at least if you are using Twitter Fabric.
So right now the general idea I have in my head is where you have a row of icons along the top representing the different pages you can click on. You click the button/icon and it takes you to that page.
A typical implementation of that in mobile apps is to use tabs that contain your icons.
Is it considered acceptable practice to use fragments here? Use one main activity that has the icon-row at the top and then have the container below that "swap out" fragments based on the icon clicked? And then each page is really just a big fragment?
Most modern tab implementations are based around using a ViewPager as the container for the tabs, so the user can swipe the content or tap on the tab to switch to different pages. ViewPager can work with plain views for its pages, but the stock PagerAdapter implementations use fragments.
Even if you elected to eschew tabs, using fragments for the pages (whether wrapped in a ViewPager or not) is reasonable.
The big thing to watch out for is memory consumption. Android devices do not have infinite RAM.[citation needed] You need to make sure that you have a modest number of fragments outstanding at any given point.
Yes, this is the proper way to use the parent activity or fragment with this "icon-row". You can use the Toolbar+menu for example, if you want to preserve the Android look, use tabs+ViewPager or use custom view.
Then, in this activity/fragment you will have a layout that will work as a fragment container. In this layout you can replace the fragments dynamically using the FragmentManager of parent activity/fragment. Each of these pages is a separate fragment.
So yes, this is good/common practice.
You may read the how-to about replacing fragments here

How to use activities or fragments in a multi screen workflow?

I have a workflow with several screens with different questions and answer-options. So there is a question on screen 1 and the user makes his choise -> the user hits the continue button -> screen 2 opens with another question -> user makes his choice and continues -> screen 3 opens etc...
But actually I'm not sure which is the best way to implement this behavior in consideration of a good maintainability and clearness. I think they are at least three options:
Every screen gets its own activity and layout file and I pass the choosen data trough intents.
1 Activity and different fragments, each fragment has its own layout. If the continue button is pressed, the fragment will be replaced with the next fragment.
1 Activity and different layout files. Just the layout gets replaced and everything else is handled in the activity.
Actually I already started implementing with option 2.) but I don't know if this is not "too much".
The Android API guidelines say that you should use Fragments for multipane UI and for reusability. But actually I don't want to build a multipane UI in this case and the fragments are not reused. So maybe 3.) is the way to go? Or even 1.)?
I would choose your option # 3. The reasons are:
Activity takes some memory compared to fragment or layouts. The interface between activities and fragments is a bit awkward, though I got used to it. I don't recommend it for your case.
I like fragments. However if all the fragments are similar in looks/feel, then why take the computer time for hiding/showing or replacing them. The reason for fragments is stated in Google web page like at Building a Flexible UI. I don't think you need a flexible UI as said in Google's intention.
I would suggest one Activity, at least one Fragment for all the questions/answers. You simply use one layout to change text/content. If the UI is different, then replace the layout with another. It's quite normal for an Android app to have so many different layouts anyway.
Android takes some memory to load layouts but it's quite fast and efficient.
For option 3 design:
You can use the inflate() method to load a certain layout and to replace one. A good example at SO link # Android layout replacing a view with another view on run time

Architecture for a drawer layout app

I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM

Categories

Resources