fragments anyone

19
Fragments anyone? Leonid Olevsky, Yossi Elkrief http://developer.android.com/guide/components/fragments.html

Upload: yossi-elkrief

Post on 14-Jul-2015

648 views

Category:

Documents


0 download

TRANSCRIPT

Fragments anyone?Leonid Olevsky, Yossi Elkrief

http://developer.android.com/guide/components/fragments.html

Content

● What is a Fragment?

● Design

● Backward compatibility

● Lifecycle

● Managing

● Transaction

● Best practices

● DialogFragment

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

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?

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

Lifecycle

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.

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();

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()

Managing

● FragmentManager○ getFragmentManager()

■ findFragmentById()

■ findFragmentByTag()

■ popBackStack()

■ addOnBackStackChangedListener()

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();

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

}}

Best Practices

● Fragment Interfaces Invoke Activity

○ Use event callbacks.

● Activity as commutator

DialogFragment

● showDialog / dismissDialog - deprecated

● floating on top of its activity's window

● v4 support library (for backward compatibility

on pre-Honeycomb devices)

DialogFragment - classimport android.support.v4.app.DialogFragment;// ...public class EditNameDialog extends DialogFragment {

private EditText mEditText;public EditNameDialog() {

// Empty constructor required for DialogFragment}@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");

return view;}

}

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 automaticallymEditText.requestFocus();

getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);mEditText.setOnEditorActionListener(this); // when we press donereturn view;

}@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

if (EditorInfo.IME_ACTION_DONE == actionId) {// Return input text to activityEditNameDialogListener activity = (EditNameDialogListener) getActivity();activity.onFinishEditDialog(mEditText.getText().toString());this.dismiss();return true;

}return false;

}

DialogFragment - Showingimport android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;// ...public class FragmentDialogDemo extends FragmentActivity implements EditNameDialogListener {

@Overridepublic 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");

}@Overridepublic void onFinishEditDialog(String inputText) {

Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();}

}

Sample Fragments application

https://github.com/MaTriXy/FragmentsGDG

Questions?

http://developer.android.com/guide/components/fragments.html