0
Fragments anyone?      Leonid Olevsky, Yossi Elkriefhttp://developer.android.com/guide/components/fragments.html
Content● What is a Fragment?● Design● Backward compatibility● Lifecycle● Managing● Transaction● Best practices● DialogFrag...
Fragment ?Fragment = partial behavior of user interface● Modular design to different screen orientations and   multiple sc...
Design● Fragments were introduced in Android 3.0● Support more dynamic and flexible UI  designs on large screens● Allow de...
Backward compatibility● Compatibility Package● Targeting 1.6 (API 4) or later● APIs work almost exactly the same as their ...
Lifecycle
Adding Fragments Via layout<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/a...
Adding Fragments Via Code● specify a ViewGroup in which to place the  fragment● using the add() method, specifying the  fr...
Adding Fragments without a UI● provide a background behavior for the  activity without presenting additional UI● it does n...
Managing● FragmentManager  ○ getFragmentManager()    ■ findFragmentById()    ■ findFragmentByTag()    ■ popBackStack()    ...
Handling fragments example● FragmentManager + FragmentTransaction● Usage:// Create new fragment and transactionFragment ne...
Fragments Special care● run-time configuration change can cause the  activity automatically re-instantiate existing  fragm...
Best Practices● Fragment Interfaces Invoke Activity  ○ Use event callbacks.● Activity as commutator
DialogFragment● showDialog / dismissDialog - deprecated● floating on top of its activitys window● v4 support library (for ...
DialogFragment - classimport android.support.v4.app.DialogFragment;// ...public class EditNameDialog extends DialogFragmen...
DialogFragment - with interface@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,      Bundle...
DialogFragment - Showingimport android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;// .....
Sample Fragments application https://github.com/MaTriXy/FragmentsGDG
Questions?http://developer.android.com/guide/components/fragments.html
Upcoming SlideShare
Loading in...5
×

Fragments anyone

635

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
635
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Fragments anyone "

  1. 1. Fragments anyone? Leonid Olevsky, Yossi Elkriefhttp://developer.android.com/guide/components/fragments.html
  2. 2. Content● What is a Fragment?● Design● Backward compatibility● Lifecycle● Managing● Transaction● Best practices● DialogFragment
  3. 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. 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. 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
  6. 6. Lifecycle
  7. 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. 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 insertFragmentManager fragmentManager = getFragmentManager()FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();ExampleFragment fragment = new ExampleFragment();fragmentTransaction.add(R.id.fragment_container,fragment);fragmentTransaction.commit();
  9. 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()
  10. 10. Managing● FragmentManager ○ getFragmentManager() ■ findFragmentById() ■ findFragmentByTag() ■ popBackStack() ■ addOnBackStackChangedListener()
  11. 11. Handling fragments example● FragmentManager + FragmentTransaction● Usage:// Create new fragment and transactionFragment 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 stacktransaction .replace(R.id.fragment_container , newFragment );transaction .addToBackStack (null);// Commit the transactiontransaction .commit();
  12. 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. 13. Best Practices● Fragment Interfaces Invoke Activity ○ Use event callbacks.● Activity as commutator
  14. 14. DialogFragment● showDialog / dismissDialog - deprecated● floating on top of its activitys window● v4 support library (for backward compatibility on pre-Honeycomb devices)
  15. 15. DialogFragment - classimport 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. 16. DialogFragment - with interface@Overridepublic 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;}@Overridepublic 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. 17. DialogFragment - Showingimport 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(); }}
  18. 18. Sample Fragments application https://github.com/MaTriXy/FragmentsGDG
  19. 19. Questions?http://developer.android.com/guide/components/fragments.html
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×