1. Fragments anyone?
Leonid Olevsky, Yossi Elkrief
http://developer.android.com/guide/components/fragments.html
2. Content
● What is a Fragment?
● Design
● Backward compatibility
● Lifecycle
● Managing
● Transaction
● Best practices
● DialogFragment
3. Fragment ?
Fragment = partial behavior of user interface
● Modular design to different screen orientations and
multiple screen sizes
● Multiple fragments can reside in a single activity
● Reuse a fragment in multiple activities
● Has its own lifecycle
4. Design
● Fragments were introduced in Android 3.0
● Support more dynamic and flexible UI
designs on large screens
● Allow designs without the need to manage
complex changes to the view hierarchy
● What about previous versions of Android?
5. Backward compatibility
● Compatibility Package
● Targeting 1.6 (API 4) or later
● APIs work almost exactly the same as their
counterparts in the latest Android platform
○ android.support.v4.app.FragmentActivity
○ android.support.v4.app.Fragment
○ android.support.v4.app.FragmentManager
○ android.support.v4.app.FragmentTransaction
7. Adding Fragments Via layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.
com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.gdg.FirstFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.gdg.SecondFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Identifiers : id, tag, id of fragment container.
8. Adding Fragments Via Code
● specify a ViewGroup in which to place the
fragment
● using the add() method, specifying the
fragment to add and the view in which to
insert
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.
beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container,
fragment);
fragmentTransaction.commit();
9. Adding Fragments without a UI
● provide a background behavior for the
activity without presenting additional UI
● it does not receive a call to onCreateView()
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(fragment, "FragmentTag");
fragmentTransaction.commit();
● get the fragment from the activity later, you
need to use findFragmentByTag()
11. Handling fragments example
● FragmentManager + FragmentTransaction
● Usage:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment ();
FragmentTransaction transaction = getFragmentManager().beginTransaction ();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction .replace(R.id.fragment_container , newFragment );
transaction .addToBackStack (null);
// Commit the transaction
transaction .commit();
12. Fragments Special care
● run-time configuration change can cause the
activity automatically re-instantiate existing
fragments.
// Tell the framework to try to keep this fragment around
// during a configuration change.
setRetainInstance(true);
● Check your Bundle.
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null){
Do Create the Fragment
}else{
Activity was recreated
}
}
13. Best Practices
● Fragment Interfaces Invoke Activity
○ Use event callbacks.
● Activity as commutator
14. DialogFragment
● showDialog / dismissDialog - deprecated
● floating on top of its activity's window
● v4 support library (for backward compatibility
on pre-Honeycomb devices)
15. DialogFragment - class
import android.support.v4.app.DialogFragment;
// ...
public class EditNameDialog extends DialogFragment {
private EditText mEditText;
public EditNameDialog() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_name, container);
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
getDialog().setTitle("Hello");
return view;
}
}
16. DialogFragment - with interface
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_name, container);
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
getDialog().setTitle("Hello");
// Show soft keyboard automatically
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setOnEditorActionListener(this); // when we press done
return view;
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (EditorInfo.IME_ACTION_DONE == actionId) {
// Return input text to activity
EditNameDialogListener activity = (EditNameDialogListener) getActivity();
activity.onFinishEditDialog(mEditText.getText().toString());
this.dismiss();
return true;
}
return false;
}
17. DialogFragment - Showing
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
// ...
public class FragmentDialogDemo extends FragmentActivity implements EditNameDialogListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showEditDialog();
}
private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
EditNameDialog editNameDialog = new EditNameDialog();
editNameDialog.show(fm, "fragment_edit_name");
}
@Override
public void onFinishEditDialog(String inputText) {
Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
}