Fragment

Before start ...

  • View

  • Activity

  • Window                                                     

What is Fragment ?

Activity child

  • An activity can manage several fragment

  • A fragment has it's lifecycle

  • Design for table

Create Fragment

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <fragment
        class="com.oreilly.demo.android.contactviewer.DateTime"
        android:id="@+id/date_time"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>
@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.main);
}

Create Fragment

public class DateTime extends Fragment {
    private String time;
    public void onCreate(Bundle state) {
        super.onCreate(state);
        if (null == time) {
            time = new SimpleDateFormat("d MMM yyyy HH:mm:ss")
            .format(new Date());
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle b) {
        View view = inflater.inflate(
        R.layout.date_time,
        container,
        false); //!!! this is important
        ((TextView) view.findViewById(R.id.last_view_time))
        .setText(time);
        return view;
    }
}

Life Cycle

  • It's own life cycle

  • OnCreate()

  • OnCreateView()

  • OnPause()

  • With Activity

  • BackStack                                                     

Fragment Manager

  • Manage fragment

  • Call getFragmentManager()     

  • add, remove, commit

  • Transaction by tag

Fragment Transaction

  • Like change activity

  • store info with argument

  • get fragment by these argument      

Support package

Android 3.0 start support fragment

Google provide support package in previous veriosn

On different device

<supports-screens
    android:smallScreens="false"
    android:normalScreens="false"
    android:largeScreens="true"
    android:xlargeScreens="true" />

Fragment

By zlsh80826

Fragment

  • 430