[android] playing with fragment
DESCRIPTION
PT.BUZOO INDONESIA is No1 Japanese offshore development company in Indonesia. We are professional of web solution and smartphone apps. We can support Japanese, English and Indonesia. We are hiring now at http://buzoo.co.id/TRANSCRIPT
![Page 1: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/1.jpg)
PLAYING WITH FRAGMENT
Android Lecture
![Page 2: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/2.jpg)
OVERVIEW• Introduced with Honeycomb (API 11)• Modular section of an activity / Part of a user interface• More dynamic and flexible UI on large screens• Reusability across activities• Supported via Compatibility Package• Embedded in activities• Lives in a ViewGroup inside the activity’s view
hierarchy• Its own layout and behavior with its own life-cycle
![Page 3: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/3.jpg)
WHEN TO USE?• Large screens - multi pane, flexible U• Combined with other UI widgets, ActionBar,
ViewPager, NavigationDrawer etc. - beautiful native app!
• More flexible, smooth, compact app with less activities. Multiple fragments in a single activity
![Page 4: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/4.jpg)
USE CASE - TABLET VS HANDSET
![Page 5: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/5.jpg)
LIFECYCLE
![Page 6: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/6.jpg)
FRAGMENT MAGEMENT• FragmentManager to interact with fragments inside
an Activity• Activity.getFragmentManager()• android.support.v4.app.FragmentActivity.getSupportFragmen
tManager()
• FragmentTransaction to perform fragment operations : add, remove, replace, hide at run-time etc.
![Page 7: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/7.jpg)
FRAGMENT COMMUNICATION• Fragment to Fragment communication via callbacks to
the Activity; inner type interfaces defined in fragments• Avoid tight coupling between activities and fragments• To access activity from fragment,
Fragment.getActivity()• To access fragment from activity
• FragmentManager.findFragmentById(int id)• FragmentManager.findFragmentByTag(String tag)
![Page 8: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/8.jpg)
FRAGMENT COMMUNICATION• Fragment to fragment communication should be done
through Parent Activity• Create inner interface Listener
public interface OnItemClickListener{
public void onItemClick(int position);
}
![Page 9: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/9.jpg)
FRAGMENT COMMUNICATION• Prepare the callback
OnItemClickListener callback;
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
callback = (OnItemClickListener) activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString() +
" must implements OnItemClickListener");
}
}
![Page 10: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/10.jpg)
FRAGMENT COMMUNICATION• Trigger when needed
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
callback.onItemClick(position);
}
![Page 11: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/11.jpg)
FRAGMENT COMMUNICATION• Passing data to fragment
DetailFragment detailFragment = new DetailFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", position);
detailFragment.setArguments(bundle);
![Page 12: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/12.jpg)
FRAGMENT COMMUNICATION• Catch data from fragment
Bundle bundle = getArguments();
int index = 0;
if(bundle != null) index = bundle.getInt("index", 0);
![Page 13: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/13.jpg)
FRAGMENT TRANSACTION• add - Add a fragment to the activity• remove - Remove an existing fragment; its view is
also removed• replace - Remove one fragment from the activity and
add another• hide - Hides an existing fragment; its view is hidden• show - Show previously hidden fragment
![Page 14: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/14.jpg)
FRAGMENT TRANSACTION
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.main, detailFragment)
.addToBackStack(null)
.commit();
![Page 15: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/15.jpg)
NESTED FRAGMENTFragment inside another fragment
FragmentTransaction transaction = detailFragment.getChildFragmentManager().beginTransaction();
transaction.add(R.id.detail, nestedFragment, "nested")
.addToBackStack(null)
.commit();
![Page 16: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/16.jpg)
FRAGMENT BACKSTACK
A AB ABC
transaction.add(R.id.main, fragmentA).addToBackStack(“fragmentA”).commit();
transaction.add(R.id.main, fragmentB);
transaction.add(R.id.main, fragmentC);
![Page 17: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/17.jpg)
FRAGMENT BACKSTACK
A AB ABC
getSupportFragmentManager(). popBackStack(“fragmentA”, 0);
getSupportFragmentManager(). popBackStackImmediate(“fragmentA”, 0);
![Page 18: [Android] PLAYING WITH FRAGMENT](https://reader035.vdocument.in/reader035/viewer/2022062703/554d2037b4c905c5208b4a54/html5/thumbnails/18.jpg)
OTHERS• DialogFragment• ViewPager• PreferenceFragment• Headless Fragment