Understanding Activities and Fragments in Android Development
This guide provides a comprehensive overview of Activities and Fragments in Android application development. Activities represent a single, focused point of interaction for users, while Fragments allow modular UI design for flexible layouts, especially on larger screens like tablets. You'll discover how to create and manage Activities, use Intents to launch them, and implement Fragment transactions to enhance your app's interface. Learn about adding, removing, and replacing Fragments, as well as best practices for inter-fragment communication and activity lifecycle management.
Understanding Activities and Fragments in Android Development
E N D
Presentation Transcript
Applications were originally built on the Activity class. AD*: “An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). “ Activity Activity Activity *AD signifies a direct quote from Android Documentation. Activity
AD: “An intent is an abstract description of an operation to be performed…Its most significant use is in the launching of activities, where it can be thought of as the glue between activities.” Intent mapIntent= new Intent(this, MapActivity.class); startActivity(mapIntent); Intent Intent Activity Activity Activity Activity
AD: “Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet's screen is much larger than that of a handset, there's more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity's appearance at runtime and preserve those changes in a back stack that's managed by the activity.” Fragment Activity Fragment Fragment Fragment
AD: “You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities). ” FragmentTransactionft = getFragmentManager().beginTransaction(); ft.replace(android.R.id.content, mMapFragment).commit(); Fragment Activity Fragment Fragment Fragment
Intent Intent Fragment Fragment Activity Another Activity Yet Another Activity An Activity with a collection of Fragments can use an Intent to launch another Activity and associated Fragments. Fragment Fragment Fragment Fragment Fragment Fragment
Reusable Fragments: • Created in the Activity • Known by the Activity • Exchange of information between Fragments is accomplished by routing information through the Activity Fragment Activity Fragment Fragment Fragment
Application Icon/Application Name Fragment Content Action Bar
Activity Fragments
Only one Fragment is shown at a time. Typical Fragment Operations: add, remove, show , hide and replace
Layout File <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/information_activity" android:orderInCategory="99" android:showAsAction="always" android:icon="@drawable/lt_action_about" android:title="@string/information_view" /> <item android:id="@+id/map_activity" android:orderInCategory="100" android:showAsAction="always" android:icon="@drawable/lt_location_map" android:title="@string/map_view"/> <item android:id="@+id/forest_description_activity" android:orderInCategory="101" android:showAsAction="always" android:icon="@drawable/lt_collections_view_as_list" android:title="@string/picture_view" /> <item android:id="@+id/pix_activity" android:orderInCategory="102" android:showAsAction="always" android:icon="@drawable/lt_content_picture" android:title="@string/pix" /> </menu> Action Bar Resource File