- onCreate()
当创建fragment时, 系统调用此方法.
在实现代码中, 应当初始化想要在fragment中保持的必要组件,当fragment被暂停或者停止后可以恢复. - onCreateView()
fragment第一次绘制它的用户界面的时候, 系统会调用此方法.为了绘制fragment的UI, 此方法必须返回一个View, 这个view是你的fragment布局的根view. 如果fragment不提供UI, 可以返回null. - onPause()
用户将要离开fragment时,系统调用这个方法作为第一个指示(然而它不总是意味着fragment将被销毁.)在当前用户会话结束之前,通常应当在这里提交任何应该持久化的变化(因为用户有可能不会返回).
- DialogFragment

显示一个浮动的对话框.
用这个类来创建一个对话框,是使用在Activity类的对话框工具方法之外的一个好的选择,
因为你可以将一个fragment对话框合并到activity管理的fragment back stack中, 允许用户返回到一个之前曾被摒弃的fragment. - ListFragment
显示一个由一个adapter(例如 SimpleCursorAdapter)管理的项目的列表, 类似于 ListActivity.
它提供一些方法来管理一个list view, 例如 onListItemClick() 回调来处理点击事件. - PreferenceFragment
显示一个 Preference对象的层次结构的列表, 类似于 PreferenceActivity.
这在为你的应用创建一个"设置"activity时有用处.
|
public static class ExampleFragment extends Fragment {
|
- 想要加载的layout的resource ID.
- 加载的layout的父ViewGroup.
传入container是很重要的, 目的是为了让系统接受所要加载的layout的根view的layout参数,
由它将挂靠的父view指定. - 布尔值指示在加载期间, 展开的layout是否应当附着到ViewGroup (第二个参数).
(在这个例子中, 指定了false, 因为系统已经把展开的layout插入到container – 传入true会在最后的layout中创建一个多余的view group.)
<?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.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
- 为 android:id 属性提供一个唯一ID.
- 为 android:tag 属性提供一个唯一字符串.
- 如果以上2个你都没有提供, 系统使用容器view的ID.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
- 使用findFragmentById() (用于在activity layout中提供一个UI的fragment)或findFragmentByTag() (适用于有或没有UI的fragment)获取activity中存在的fragment
- 将fragment从后台堆栈中弹出, 使用 popBackStack() (模拟用户按下BACK 命令).
- 使用addOnBackStackChangeList
ener()注册一个监听后台堆栈变化的listener.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 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();
- 必须最后调用 commit().
- 如果添加多个fragment到同一个容器, 那么添加的顺序决定了它们在view hierarchy中显示的顺序.
View listView = getActivity().findViewById(R.id.list);
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
public static class FragmentA extends ListFragment {
...
// Container Activity must implement this interface
public interface OnArticleSelectedListene r {
public void onArticleSelected(Uri articleUri);
}
...
}
public static class FragmentA extends ListFragment {
OnArticleSelectedListene r mListener;
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListene r) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListene r");
}
}
...
}
public static class FragmentA extends ListFragment {
OnArticleSelectedListene r mListener;
...
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Append the clicked item's row ID with the content provider Uri
Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
// Send the event and Uri to the host activity
mListener.onArticleSelected(noteUri);
}
...
}
- Resumed
在运行中的activity中fragment可见. - Paused
另一个activity处于前台并拥有焦点, 但是这个fragment所在的activity仍然可见(前台activity局部透明或者没有覆盖整个屏幕). - Stopped
要么是宿主activity已经被停止, 要么是fragment从activity被移除但被添加到后台堆栈中.
停止状态的fragment仍然活着(所有状态和成员信息被系统保持着). 然而, 它对用户不再可见, 并且如果activity被干掉,他也会被干掉.
- onAttach()
当fragment被绑定到activity时被调用(Activity会被传入.). - onCreateView()
创建和fragment关联的view hierarchy时调用. - onActivityCreated()
当activity的onCreate()方法返回时被调用. - onDestroyView()
当和fragment关联的view hierarchy正在被移除时调用. - onDetach()
当fragment从activity解除关联时被调用.
3486



被折叠的 条评论
为什么被折叠?



