View on GitHub

Bearded-android-docs

Fragment

Download this project as a .zip file Download this project as a tar.gz file

Created Sunday 14 July 2013

A fragment as a modular section of an activity which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

Think of a fragment as a sub-activity.

Fragments can:

Difference Between Activity Life Cycle

onAttach()
Called when the fragment has been associated with the activity (the Activity is passed in here).
onCreateView()
Called to create the view hierarchy associated with the fragment.
onActivityCreated()
Called when the activity's onCreate() method has returned.
onDestroyView()
Called when the view hierarchy associated with the fragment is being removed.
onDetach()
Called when the fragment is being disassociated from the activity.

Communication Between Parent

+ Define an interface in the fragment
+ Have the parent implement the interface
+ Override onAttach() in the fragment and save a reference to the activity (which should implement) the interface
+ Call the saved interface when you need to talk to the parent

To communicate the other way, use the FragmentManager.
For example:

MyFragment fragment = (MyFragment) getFragmentManager()
            .findFragmentById(R.id.myfragment);

Links

Tags


No backlinks to this page.
comments powered by Disqus