1/14

Android Development

Android Framework Basics

2/14

Agenda

3/14

Fragment Basics

4/14

Fragment View Layout

5/14

Frament View Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentexampleapp.ExampleListFragment"
        android:layout_width="248dp"
        android:layout_height="match_parent" />
    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragmentexampleapp.ExampleDetailFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>
6/14

Fragment Implementation

public class ExampleDetailFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		return inflater.inflate(R.layout.detail_fragment,container, false);
	}
}
7/14

Fragment Implementation

√úBERARBEITEN !!!! -> List ersetzten mit Fragment

public class ExampleListFragment extends ListFragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		List<String> songs = Arrays.asList("Blur","Metallica","Bosse");
		ArrayAdapter<String> bandArray = new ArrayAdapter<String>
			(getActivity(), android.R.layout.simple_list_item_1,songs);
		setListAdapter(bandArray);
	}
}
8/14

Fragment Lifecycle

Fragments exist in three states when started:

  • Resumed: Fragment is visible and running
  • Paused: Another Activity is in theforeground and has focus but the Fragment is still visible
  • Stopped: Fragment is not visible but still alive. Either Activity has been stopped or Fragment removed

The lifecycle of the Activity directly affects the lifecycle of the Fragment

40%

9/14

Fragment Lifecycle

Fragment specific lifecycle callbacks are:

  • onAttach(): called when Fragment is added to Activity
  • onCreateView(): creates the view hierarchy of the Fragment
  • onActivityCreated(): called when Activity’s onCreate() method returned
  • onDestroyView(): called when view hierarchy of Fragement is removed
  • onDetach(): called when Fragment is disassociated with Activity

40%

10/14

Adding / Replacing Fragments dynamically

11/14

Adding / Replacing Fragments dynamically

ArticleFragment newFragment = new ArticleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

/* Replace whatever is in the fragment_container view with this fragment,
and add the transaction to the back stack so the user can navigate back */
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
12/14

Android Assignment 4

What you will learn

+

13/14

Summary

14/14

Recab Questions