[android] playing with fragment

18
PLAYING WITH FRAGMENT Android Lecture

Upload: jun-shimizu

Post on 09-May-2015

751 views

Category:

Technology


0 download

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

PLAYING WITH FRAGMENT

Android Lecture

Page 2: [Android] PLAYING WITH FRAGMENT

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

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

USE CASE - TABLET VS HANDSET

Page 5: [Android] PLAYING WITH FRAGMENT

LIFECYCLE

Page 6: [Android] PLAYING WITH FRAGMENT

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

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

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

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

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

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

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

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

FRAGMENT TRANSACTION

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

transaction.add(R.id.main, detailFragment)

.addToBackStack(null)

.commit();

Page 15: [Android] PLAYING WITH FRAGMENT

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

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

FRAGMENT BACKSTACK

A AB ABC

getSupportFragmentManager(). popBackStack(“fragmentA”, 0);

getSupportFragmentManager(). popBackStackImmediate(“fragmentA”, 0);

Page 18: [Android] PLAYING WITH FRAGMENT

OTHERS• DialogFragment• ViewPager• PreferenceFragment• Headless Fragment