So I have several fragments connected to a viewpager that is just on the generic android set up that comes with Eclipse.
I was wondering; when it comes to updating element views for things like progress bars or textviews; should I update those elements inside of the fragment's .java file or should I update them from some sort of centralized UI controller object?
The way I have it set up now is a controller object with several async tasks inside of it for each fragment.
Well the updation of views inside fragment based applications requires activity to fragment, fragment to fragment communication. So for this you need the event based mechanism so that the your target view/controller get to know that something has happened like say download is complete so hide the progressbar.
So I googled regarding this. I found one libray Otto eventbus. Which helped me in such communications. Hope you get the idea and this will help you as well.
Related
I have an android app where I want to have different functionalities, like a search function, a history, user defined lists,... Each of these functionalities is accessible via NavigationDrawer, and I want to contain a ViewPager to allow navigation in Tabs in each of those specific functionalities. Finally, when I switch between functions via NavDrawer I want my app to restore the state the corresponding function was in the last time it was used.
I'm puzzled about the best way to implement this. Currently, I'm using one Activity, which contains the logic for functionality navigation, and saving and restoring of current state of each fragment. I'm using one ViewPagerAdapter to hold information about current active fragments.
Another possible approach would be to make an Activity for each top-level functionality, and then implement the Fragments as needed. A third approach I can think of would be still using only one Activity, but different ViewPagerAdapter to hold the state information of each top-level function.
I'm really not sure which is the best way to go here, and I would like to settle on one of those possible options before going further, but I can't really find any useful information on the net. Are there any Best Practices known for my given scenario, or is this really just an opinion based topic?
Edit: No duplicate of Difference between Activity and FragmentActivity because I'm not even tackling the issue of support library vs. native
Edit2: I think it should be also possible by using nested fragments, i.e. having a fragment which hold my ViewPager where I could add my fragments for that function. But I really would prefer not using nested fragments at all.
A Fragment is best thought of as "a group of reusable interface elements". So if you can think of reusable elements across the activities (or a single activity when it's recreated) then you use fragments.
Activity is best thought of as "a single screen with UI". So then..
I have two different Activities, and I want to show them both at the same time in another Activity. Is there any way to do that?
Thanks in advance!
its not possible , You have to use Fragments to do so...
convert those two Activities to Fragments and create a Activity and place those Fragments in Activity.
Ref :
http://developer.android.com/reference/android/app/Fragment.html
http://developer.android.com/guide/components/fragments.html
Yes, it is possible with ActivityView in android. There is no proper documentation that is available on the internet. but if you can check the android source code you can able to find the code related to ActivityView. Android is using the ActivityView for its car launcher applications.
check out below links:
google implementation of ActivityView.java
you can find the ActivityView.java usage examples
here
and here
Yes there is a way ! You cannot display 2 activity inside one, you have to use fragments !
A fragment is an independent component which can be used by an activity. A fragment encapsulates functionality so that it is easier to reuse within activities and layouts.
A fragment runs in the context of an activity, but has its own life cycle and typically its own user interface. It is also possible to define fragments without an user interface, i.e., headless fragments.
Fragments can be dynamically or statically added to an activity.
You'll find the answers of all of your questions about fragments on this link
Hope it's been helpfull =)
Fragments and FragmentActivity comes into play here. Make your two activities separately as two different fragments, And attach these two fragments into a FragmentActivity. So that in a single screen you can view two activites(Here its a fragment).
You can also try using ViewPager concept it may suits to your scenario.
Download the sample here,
http://developer.android.com/training/animation/screen-slide.html
OK, I get the concept of Fragments being modular/ interchangeable "sub-activities" but given that fragments have their own lifecycle much like the one of an activity and can pretty much do anything that an activity can, from a best-practice standpoint, does that mean we are to start coding a single activity for the whole app (say, the main_activity) and simply adding/ replacing/ removing fragments within that single activity?
If not, then how do I know when it's time to create a new activity instead of continuing to push new fragments into the same old activity?
Just trying to understand how to best organize my app's features into activities and frags. A practical example would help (no code necessary, just the concepts).
Cheers,
No, I does not mean that you should use single activity.
Fragments are to help you organize ui elements (especially on big screens (like tablets)).
They also introduce new layer of reusability (using fragments for loaders, simple views) across your projects.
I recomend you check Google NewsReader SDK example which is a great way to see how to implement activity/fragment patterns depending on what type of screen it is launched on
http://developer.android.com/training/multiscreen/adaptui.html
Download sample button is on the right
No fragments are just one way to reuse parts of an application. For instance you can use a fragment in several activities. On the other hand you can of course have several activities that use different fragments or do not use fragments at all.
So summary: no you don't need to. Fragments are helpful, if you build up multiple activities that use a similar or equal component as a part of their layout.
I am using setContentView(R.layout.main) to switch the views in the same activity. I am calling some asynchronous task and populating the data on the main layout file after that I am changing the view by calling setContentView(R.layout.main) method.
I came to know that we should not use setContentView method multiple times for same activity. Though it is working fine for me.
Can anyone explain why we should not use setContentView method multiple times for the same activity to change the views?
Will it create any memory related exceptions? Could someone please clarify?
I think switching Views is not a good idea, because android platform already have strong framework to handle the transition in between the views and maintaining the state of each view associated with the Activity its always better to stick with the existing framework instead of thinking of some complex implementation that you have to go through to do all these things. If you do not need any of these things to taken care in your application and if only if you have only two or three screen in your entire application you can try switching the views. That even based on how your views are structured if you have complex logic and lot of data needed to create these views this wont be a good way of doing it.One more thing if you are adding more views say functionality to your application the load that need to be handled by the Activity will go high. In this case you will be declaring and initializing all views inside that particular Activity so maintaining all these views instances is heavy. If you want to know more about the Activty and Task kindly refer this link
Well every time you call setContentView() you'll have to find all the layouts again besides that I think you "can" do it. But as discussed here this is ill adviced as it clearly goes against the android guidelines. Also Commonsware have some very important points here one of the most important being that you will be prone to leak memory as you forget to clean up stuff from your views etc. which Android normally would handle for you.
In short you should follow Android guidelines and use Fragments or start a new Activity.
According to the developer docs setContentView(int layoutResID) is used to
Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
In best practice this method is used to Inflate your Activity layout on start up. This does not mean that it will cause issues in the future if you keep using this method. To quote a answer in this question
The setContentView on your Activity actually calls the setContentView on the Window used by the activity, which itself does a lot more than just inflating the layout.
I suggest that you find a alternative way to switch layouts like using a ViewPager with Fragments or some other Tabbing approach but in the end it all comes down to what you want to do.
This question might also give you what you're looking for.
I am making my first android application with the ActionBarSherlock.
The application will always have an action bar consisting of 3 tabs (first tab selected by default).
The app could be extended for use with a tablet.
I have been searching the web, and following the android development guides, however I am finding a few things confusing.
The first tab screen will be a list view with a list of items, onitemselected should send the user to a screen which features more details about that item.
When should I use a fragment? Should each tab be a fragment?
Or, should each tab call a new activity, which consists of fragments?
And, if using fragments, should I place them in different classes, or embed them within an activity??
Appreciate any help, thanks.
you should probably read these two links first.
http://android-developers.blogspot.com/2011/09/preparing-for-handsets.html
http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
If you plan to make an app that work on both phone and tablet. It is a good idea to use a fragment, and then use a shell activity to wrap that fragment.
My experience with Fragments is mostly on ViewPager, so I am not entirely sure if it applies here.
In Android, you should use Fragments as much as possible. As a general rule of thumb, imagine you are translating the UI from phones to tablets, elements that can stay together in the same configuration should be a Fragment.
There is a Fragment subclass called ListFragment, so you might want to look into that for your first tab. (ListFragment is for Fragment like ListActivity is for Activity)
There is also a tutorial I found to deal with fragments. Did not really look into it but I hope it helps.
http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/
As for ActionBar / ActionBarSherlock, I have absolutely no experience withit so someone might want to add to that.