Presented at Big Android BBQ 2015
Hurst Convention Center, Hurst Texas
This talk will cover Fragments in detail by comparing and contrasting them to something we know well, Activities. We will also cover examples and use cases. Fragments: Why, How, and What For? is targeted toward developers who may not have had a lot of experience using Fragments and those who want to understand them better. Why did Google introduce Fragments? Aren’t Activities enough? How do Fragments work? What For? Example and use cases such as Fragment reuse, single pane vs multi-pane, ViewPager, NavigationDrawer and DialogFragment.
17. public static MyFragment newInstance(int position) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(ARG_POSITION, position);
fragment.setArguments(args);
return fragment;
}
New Instances
18. public static MyFragment newInstance(int position) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(KEY_POSITION, position);
fragment.setArguments(args);
return fragment;
}
New Instances
19. Adding a Fragment
MyFragment fragment = MyFragment.newInstance(args);
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
20. Adding a Fragment
MyFragment fragment = MyFragment.newInstance(args);
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
21. Replacing a Fragment
MyFragment fragment = MyFragment.newInstance(args);
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
39. Dual or Single Pane?
if (findViewById(R.id.puppy_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
... code specific to two pane layout
}
44. Setting up our ViewPager
ViewPagerAdapter adapter =
new ViewPagerAdapter(getSupportFragmentManager());
/* add the Fragments to the ViewPagerAdapter */
for(String title : mPuppyTitles) {
adapter.addFrag(PuppyFragment.newInstance(index++), title);
}
mViewPager.setAdapter(adapter);
51. Fragment fragment = DetailFragment.newInstance(puppy, position);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
// do replace and commit operation
transaction.replace(R.id.detail_container, fragment).commit();
52. Fragment fragment = DetailFragment.newInstance(puppy, position);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
// do replace, add to backstack and commit operation
transaction.replace(R.id.detail_container, fragment)
.addToBackStack(“details”) // optional name for this back stack state, or null
.commit();
68. public class MainActivity extends AppCompatActivity implements
SampleDialogFragment.OnDialogDismissed {
// ... other methods and implementation not shown
@Override
public void onDialogDismissed(String whichSalutation) {
if (whichSalutation.equals(SampleDialogFragment.GOODBYE)) {
Toast.makeText(this, "Thank you!", Toast.LENGTH_SHORT).show();
}
}
}
69. @Override
public void onClick(DialogInterface dialog, int which) {
// display a toast when the dialog is dismissed for GOODBYE only
if (mSalutation.equals(GOODBYE)) {
mDialogDismissedCallback.onDialogDismissed(GOODBYE);
}
}