Post on 09-May-2015
description
transcript
PLAYING WITH FRAGMENT
Android Lecture
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
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
USE CASE - TABLET VS HANDSET
LIFECYCLE
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.
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)
FRAGMENT COMMUNICATION• Fragment to fragment communication should be done
through Parent Activity• Create inner interface Listener
public interface OnItemClickListener{
public void onItemClick(int position);
}
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");
}
}
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);
}
FRAGMENT COMMUNICATION• Passing data to fragment
DetailFragment detailFragment = new DetailFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", position);
detailFragment.setArguments(bundle);
FRAGMENT COMMUNICATION• Catch data from fragment
Bundle bundle = getArguments();
int index = 0;
if(bundle != null) index = bundle.getInt("index", 0);
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
FRAGMENT TRANSACTION
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.main, detailFragment)
.addToBackStack(null)
.commit();
NESTED FRAGMENTFragment inside another fragment
FragmentTransaction transaction = detailFragment.getChildFragmentManager().beginTransaction();
transaction.add(R.id.detail, nestedFragment, "nested")
.addToBackStack(null)
.commit();
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);
FRAGMENT BACKSTACK
A AB ABC
getSupportFragmentManager(). popBackStack(“fragmentA”, 0);
getSupportFragmentManager(). popBackStackImmediate(“fragmentA”, 0);
OTHERS• DialogFragment• ViewPager• PreferenceFragment• Headless Fragment