I'm upgrading an Android application that used the Dashboard pattern to the Sliding Menu, with a library by jfeinstein.
Originally I had a DashboardActivity that had 6 buttons, each of which started its corresponding Activity on tap. I also have about 30 other activities that can be started from those "top-level" activities.
Now, I have two possible approaches:
When looking at the examples for the sliding menu library, I got the idea that it might be the best approach, as it used switching fragments, but I still think that an Activity is better for a screen than a Fragment.
Is there any reason to use single activity and fragments for content?
If you want to go back from Activity to Fragment. This is very simple just override onBackPressed() in your activity and call onBackPressed where you want.
(adapted from the interwebs and Glenn Bech's answer on SO)
Fragments are Android's solution to creating reusable user interfaces. You can achieve some of the same things using activities and layouts (for example by using includes). However; fragments are wired in to the Android API, from HoneyComb, and up.
There is a lot of additional responsibility to an Activity since its a primary component. Like providing a context, etc. Without these additional responsibilities, fragments are lightweight and are especially beneficial as you have many of them (when activities are refactored to fragments). Did I mention they are lightweight?
The ActionBar. If you want tabs up there to navigate your app, you quickly see that ActionBar.TabListener inferface gives you a FragmentTransaction as an input argument to the onTabSelected method. You could probably ignore this, and do something else and clever, but you'd be working against the API, not with it.
The FragmentManager handles "back" for you in a very clever way. Back does not mean back to the last activity, like for regular activities. It meeans back to the previous fragment state. This is more awesomed (yes, its a word) by the fact that you can control when and how the backstack is used.
Where the hell do you think all the Hollywood effects come from eh? You can use the cool ViewPager with a FragentPagerAdapter to create swipe interfaces. The FragmentPagerAdapter code is much cleaner than a regular adapter, and it controls instantiations of the individual fragments. The transition and swipe animations you can apply to fragments are a few things you can't do with Activities.
Bigger Phone? No it's a Tablet. Your life will be a lot easier if you use Fragments when you try to create applications for both phones and tablets. Since the fragments are so tied in with the Honeycomb+ APIs, you will want to use them on phones as well to reuse code. That's where the compatibility library comes in handy.
You even could and should use fragments for apps meant for phones only. If you have portability in mind. I use ActionBarSherlock and the compatibility libraries to create "ICS looking" apps, that look the same all the way back to version 1.6. You get the latest features like the Actionbar, with tabs, overflow, split action bar, viewpager etc.
The best way to communicate between fragments are intents. When you press something in a Fragemnt you would typically call StartActivity() with data on it. The intent is passed on to all fragments of the activity you launch. It's just easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With