MobAppDev
Fragments & ViewGroups, Fragment
Management, Fragment Creation,
Activity vs Fragment Lifecycles
Vladimir Kulyuki...
Outline
●
Fragments & ViewGroups
●
Fragment Management
●
Fragment Creation
●
Activity vs Fragment Lifecycle
Fragments & ViewGroups
Fragment Lifecycle
source image at http://developer.android.com/guide/components/fragments.html
Fragments & ViewGroups
●
When a fragment is added to an activity layout, it
becomes a component in a ViewGroup (a containe...
Example: ListFragment Inside LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android...
Fragments & ViewGroups
●
Fragments are not required to be a part of the activity layout; there
may be fragments w/o UIs as...
Fragments on Tablets
Here is a useful link to the design and implementation
practices of how to design fragments for reuse...
Fragment Management
Managing Fragments
●
Fragments are managed with the FragmentManager class
●
FragmentManager object can be obtained via Act...
Fragment Transactions
●
FragmentTransactions are used to add, remove,
replace, and perform other actions with them, in
res...
Beginning & Ending Transactions
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransa...
A Typical Transaction Sequence
// Create new fragment and transaction
Fragment newFrgmnt = new SomeFragment();
FragmentTra...
Fragment Creation
Fragment Creation
●
To create a fragment, the developer must either create a
subclass of Fragment or a subclass of an exis...
Fragment Layouts
●
To provide a layout for a fragment, the onCreateView()
callback must be implemented
●
Android system ca...
Fragment Layouts
●
To provide a layout for a fragment, the onCreateView()
callback must be implemented
●
Android system ca...
Programmatic Addition of Fragments
// getFragmentManager() is a method of the Activity class
FragmentManager fragmentManag...
Order of Multiple Fragments
●
A call to commit() must be last
●
When multiple fragments are added to the same
container, t...
Finding Fragments w/in Activity
FragmentManager fm = getFragmentManager();
SomeFragment fragment =
(SomeFragment) fm.findF...
Finding Fragments w/in Activity
FragmentManager fm = getFragmentManager();
SomeFragment fragment =
(SomeFragment) fm.findF...
Three Important States
●
Resumed: fragment is visible
●
Paused: another activity is in the foreground
and has focus.
●
Sto...
Activity vs Fragment Lifecycles
source image at http://developer.android.com/guide/components/fragments.html
Rumi's Quatrains with Fragments
source code is here
Application Class Contents
●
RumiQuatrainMainActivity.java – main activity
●
QuatrainNumberListFragment.java – ListFragmen...
AndroidManifest.xml
<application android:icon="@drawable/rumi" android:label="Rumi's Quatrains">
<activity android:name="o...
XML Layout of the Main Activity: Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android...
XML Layout of the Secondary Activity: Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
an...
XML Layout of the Secondary Activity: Main.xml
<LinearLayout>
<ScrollView android:id="@+id/scvQuatrainText"
android:layout...
RumiQuatrainMainActivity.java
public class RumiQuatrainMainActivity extends Activity {
static final String LOGTAG = RumiQu...
RumiQuatrainMainActivity.java
●
The real workhorse of is
displayQuatrainText(int quatrain_index)
●
It finds a fragment by ...
References
●
http://developer.android.com/guide/components/fragments.html
●
S. Komatineni & D. MaClean. Pro Android 4. APR...
Upcoming SlideShare
Loading in …5
×

MobAppDev: Fragments & ViewGroups, Fragment Management, Fragment Creation, Activity vs Fragment Lifecycle

922
-1

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
922
On Slideshare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MobAppDev: Fragments & ViewGroups, Fragment Management, Fragment Creation, Activity vs Fragment Lifecycle

  1. 1. MobAppDev Fragments & ViewGroups, Fragment Management, Fragment Creation, Activity vs Fragment Lifecycles Vladimir Kulyukin www.vkedco.blogspot.com
  2. 2. Outline ● Fragments & ViewGroups ● Fragment Management ● Fragment Creation ● Activity vs Fragment Lifecycle
  3. 3. Fragments & ViewGroups
  4. 4. Fragment Lifecycle source image at http://developer.android.com/guide/components/fragments.html
  5. 5. Fragments & ViewGroups ● When a fragment is added to an activity layout, it becomes a component in a ViewGroup (a container that may contain other views called children) inside the activity's view hierarchy ● A fragment may define its own view layout ● A fragment can be inserted into the activity's layout file via the tags <fragment></fragment>
  6. 6. Example: ListFragment Inside LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="org.vkedco.mobappdev.rumi_quatrain_fragments.QuatrainNumberListFragment" android:id="@+id/quatrain_numbers" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
  7. 7. Fragments & ViewGroups ● Fragments are not required to be a part of the activity layout; there may be fragments w/o UIs as background workers for activities ● A Fragment should be designed as a modular and reusable activity component ● Why? Since fragments may define their own layouts and have their own lifecycle callbacks, one fragment may be included in multiple activities ● Direct manipulation of one fragment from another fragment should be avoided: breaks modularity and leads to cycle-related errors
  8. 8. Fragments on Tablets Here is a useful link to the design and implementation practices of how to design fragments for reuse if you want to develop for multiple screen sizes: http://developer.android.com/guide/practices/tablets-and-handsets.html
  9. 9. Fragment Management
  10. 10. Managing Fragments ● Fragments are managed with the FragmentManager class ● FragmentManager object can be obtained via Activity. getFragmentManager() ● FragmentManager is used to: – Find fragments in an activity either with findFragmentById() (for fragments that have IDs) or findFragmentByTag() (for fragments that do not have Uis) – Pop fragments off the back stack, with popBackStack() (this is the same as the user's pressing Back button) – Register a listener for changes to the back stack, with addOnBackStackChangedListener() – open FragmentTransaction objects to add and remove fragments
  11. 11. Fragment Transactions ● FragmentTransactions are used to add, remove, replace, and perform other actions with them, in response to UI gestures ● A transaction is a set of changes that must be committed with respect to the activity ● It is also possible to save each transaction to a back stack managed by the activity ● The user can navigate navigate back through the fragment changes
  12. 12. Beginning & Ending Transactions FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // some code // each transaction must be committed to take effect fragmentTransaction.commit();
  13. 13. A Typical Transaction Sequence // Create new fragment and transaction Fragment newFrgmnt = new SomeFragment(); FragmentTransaction trans = getFragmentManager().beginTransaction(); // 1) replace the contents of the view container with the newly created fragment, // 2) add the transaction to the back stack trans.replace(R.id.view_container, newFrgmnt); trans.addToBackStack(null); // Commit the transaction transaction.commit();
  14. 14. Fragment Creation
  15. 15. Fragment Creation ● To create a fragment, the developer must either create a subclass of Fragment or a subclass of an existing subclass of Fragment ● The Fragment class has code that looks a lot like an Activity in that it contains callbacks such as onCreate(), onStart(), onPause(), & onStop() ● In many cases, converting existing activities to fragments is as simple as moving code from the activities' callback methods into the corresponding methods of fragments
  16. 16. Fragment Layouts ● To provide a layout for a fragment, the onCreateView() callback must be implemented ● Android system calls this method when the fragment is ready to draw its layout ● The implementation of this method must return the View root of the fragment's layout ● Fragment typically implement UI parts to the host activity
  17. 17. Fragment Layouts ● To provide a layout for a fragment, the onCreateView() callback must be implemented ● Android system calls this method when the fragment is ready to draw its layout ● The implementation of this method must return the View root of the fragment's layout ● Fragment typically implement UI parts to the host activity
  18. 18. Programmatic Addition of Fragments // getFragmentManager() is a method of the Activity class FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // Create a fragment and use the transaction to add the fragment to the view container SomeFragment frgmnt = new SomeFragment(); fragmentTransaction.add(R.id.view_container, frgmnt); fragmentTransaction.commit();
  19. 19. Order of Multiple Fragments ● A call to commit() must be last ● When multiple fragments are added to the same container, then the order in which they are added determines the order they appear in the view hierarchy ● If there is no call to addToBackStack() in a transaction removes a fragment, the removed fragment is destroyed after the transaction is committed and the user cannot navigate back to it
  20. 20. Finding Fragments w/in Activity FragmentManager fm = getFragmentManager(); SomeFragment fragment = (SomeFragment) fm.findFragmentById(R.id.some_fragment);
  21. 21. Finding Fragments w/in Activity FragmentManager fm = getFragmentManager(); SomeFragment fragment = (SomeFragment) fm.findFragmentById(R.id.some_fragment);
  22. 22. Three Important States ● Resumed: fragment is visible ● Paused: another activity is in the foreground and has focus. ● Stopped: fragment is not visible
  23. 23. Activity vs Fragment Lifecycles source image at http://developer.android.com/guide/components/fragments.html
  24. 24. Rumi's Quatrains with Fragments source code is here
  25. 25. Application Class Contents ● RumiQuatrainMainActivity.java – main activity ● QuatrainNumberListFragment.java – ListFragment associated with main activity ● QuatrainTextDisplayActivity.java – host activity for QuatrainTextDisplayFragment.java ● QuatrainTextDisplayFragment.java – fragment to display quatrain texts
  26. 26. AndroidManifest.xml <application android:icon="@drawable/rumi" android:label="Rumi's Quatrains"> <activity android:name="org.vkedco.mobappdev.rumi_quatrain_fragments.RumiQuatrainMainActivity" android:label="Rumi's Fragments"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="org.vkedco.mobappdev.rumi_quatrain_fragments.QuatrainTextDisplayActivity" android:label="Quatrain's Text"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application>
  27. 27. XML Layout of the Main Activity: Main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="org.vkedco.mobappdev.rumi_quatrain_fragments.QuatrainNumberListFragment" android:id="@+id/quatrain_numbers" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
  28. 28. XML Layout of the Secondary Activity: Main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="org.vkedco.mobappdev.rumi_quatrain_fragments.QuatrainNumberListFragment" android:id="@+id/quatrain_numbers" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
  29. 29. XML Layout of the Secondary Activity: Main.xml <LinearLayout> <ScrollView android:id="@+id/scvQuatrainText" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvQuatrainText" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView> </LinearLayout>
  30. 30. RumiQuatrainMainActivity.java public class RumiQuatrainMainActivity extends Activity { static final String LOGTAG = RumiQuatrainMainActivity.class.getSimpleName() + "_TAG"; static Resources mRes = null; static FragmentManager mFrgmntMngr = null; static RumiQuatrainMainActivity mThisAct = null; public void onCreate(Bundle savedInstanceState) { Log.d(LOGTAG, "onCreate()"); super.onCreate(savedInstanceState); FragmentManager.enableDebugLogging(true); setContentView(R.layout.main); mRes = getResources(); mFrgmntMngr = getFragmentManager(); mThisAct = this; }
  31. 31. RumiQuatrainMainActivity.java ● The real workhorse of is displayQuatrainText(int quatrain_index) ● It finds a fragment by id and replaces it with a newly created fragment ● It also shows how to check the orientation ● If the orientation is landscape, then two fragments are displayed ● If the orientation is portrait, then a new activity is launched
  32. 32. References ● http://developer.android.com/guide/components/fragments.html ● S. Komatineni & D. MaClean. Pro Android 4. APRESS ● R. Meier. Pro Android 4 Application Development. WROX

×