Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fragments anyone ?

Fragments anyone ?

What is a Fragment?
Design
Backward compatibility
Lifecycle
Managing
Transaction
Best practices
DialogFragment

Avatar for Yossi Elkrief

Yossi Elkrief

March 08, 2013
Tweet

More Decks by Yossi Elkrief

Other Decks in Programming

Transcript

  1. Content • What is a Fragment? • Design • Backward

    compatibility • Lifecycle • Managing • Transaction • Best practices • DialogFragment
  2. 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
  3. 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?
  4. 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
  5. 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.
  6. 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();
  7. 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()
  8. 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();
  9. 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 } }
  10. DialogFragment • showDialog / dismissDialog - deprecated • floating on

    top of its activity's window • v4 support library (for backward compatibility on pre-Honeycomb devices)
  11. 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; } }
  12. 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; }
  13. 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(); } }