Reputation: 2838
pttI just started with Android development and some stuff confuses me - especially coming from iOS. As an iOS developer i normally could use the boilerplate code created for me by the IDE, however as i'm now trying to work with Android ActionBar, Tab Mode and Fragments many Tutorials seem to be different from the created Android code. So i ask myself what for my usecase the right solution would be. The boilerplate code includes:
TabActivity extends Activity implements ActionBar.TabListener
SectionsPagerAdapter
ViewPager
PlaceholderFragment extends Fragment
I have the following questions:
1) Is the setup with Activity implements ActionBar.TabListener
, SectionsPagerAdapter
and ViewPager
practical? I've seem quite some tutorials working with other soltions i.e. http://www.cs.dartmouth.edu/~campbell/cs65/lecture08/lecture08.html
2) Is the PlaceholderFragment
with it's contructor pattern for tabs advisable or should i create own fragment classes for each one of them?
3) I need to update the UI of a summary tab when some action in another Tab was triggered. I image i should be doing this at the moment the Tab is shown. However with the setup as described i couldn't find an obvious (at least to me) way to do it. Is it better to store references to each tabs UI or Fragment globally and access it directly? I think the adapter wipes its fragments from memory and on occasion reloads them.
Thanks for clearing that up to me :)
Upvotes: 0
Views: 442
Reputation: 14409
1) Take a look at the Android developers page:
2) If I'm not wrong probably you will have the PlaceholderFragment as an inner class. I recommend you to create your own fragment classes for each one of them. PlaceholderFragment is just a factory for fragments for demo proposes.
3) I would do it as soon as the action is triggered, not when you switch to that tab. The reason is that you save time and the user won't notice anything loading or wait times. But it really depends on how often the user navigates thru that particular tab.
One way to achieve this is sending a message from one fragment to another fragment. The way I did it is with local broadcasters. Is really easy to use.
Basically what it does is when you trigger the action from one particular fragment, you send a local broadcast message that is receive by the other fragment. In that moment you can call any method that you want in the second fragment but be sure to do it in background thread (asyncTask for example).
Here you have some useful links about Local Broadcast:
Pro tip: Very useful tip working with actionbar/nav tabs/fragments when you have only a few tabs (for example 3/4) is avoiding to reload them everytime you go away and come back to them. For example in your summary tab probably you process some data to come up with summary data. Data data should be processed each time the data changes. With the default configuration of the viewpager what is going to happen is that the fragment will be destroyed and recreated each time you swipe 2 tabs away.
To avoid this you have to do this:
viewPager.setOffscreenPageLimit(C_FRAGMENTS_TO_KEEP_IN_MEMORY);
C_FRAGMENTS_TO_KEEP_IN_MEMORY is the number of tabs at right and left of the current selected tab to keep in memory. The default value is 1.
Hope it helps!!
Upvotes: 1