(Android) Fragment
来源:互联网 发布:c语言编写九九乘法表 编辑:程序博客网 时间:2024/05/05 16:40
Fragment
The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
onAttach(Activity)
called once the fragment is associated with its activity.onCreate(Bundle)
called to do initial creation of the fragment.onCreateView(LayoutInflater, ViewGroup, Bundle)
creates and returns the view hierarchy associated with the fragment.onActivityCreated(Bundle)
tells the fragment that its activity has completed its ownActivity.onCreate()
.onViewStateRestored(Bundle)
tells the fragment that all of the saved state of its view hierarchy has been restored.onStart()
makes the fragment visible to the user (based on its containing activity being started).onResume()
makes the fragment interacting with the user (based on its containing activity being resumed).
As a fragment is no longer being used, it goes through a reverse series of callbacks:
onPause()
fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.onStop()
fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.onDestroyView()
allows the fragment to clean up resources associated with its View.onDestroy()
called to do final cleanup of the fragment's state.onDetach()
called immediately prior to the fragment no longer being associated with its activity.
1) Create one class extends Fragment
public class FragmentA extends Fragment {
private View parentView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.activity_group_list, null);
//Use parentView to get other views.
View view = (View)parentView.findViewById(...);
return parentView;
}
}
2) Layout xml for the fragment
layout. xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragmentcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background" />
</LinearLayout>
3) Replace the fragment in the activity
private FragmentManager fragmentMgr;
private FragmentTransaction ft;
if (fragmentMgr == null)
fragmentMgr = getSupportFragmentManager();
FragmentA fragment = fragmentMgr.findFragmentByTag("A") != null ? (FragmentA) fragmentMgr
.findFragmentByTag("A") : new FragmentA();
private void switchToCurrentFragment(Fragment frag, String tag) {
ft = fragmentMgr.beginTransaction();
ft.replace(R.id.fragmentcontainer, frag, tag);
if (!isFinishing()) {
ft.commitAllowingStateLoss();
}
}
- Android Fragment---创建Fragment
- Android Fragment---管理Fragment
- Android Fragment---创建Fragment
- Android Fragment---管理Fragment
- (Android) Fragment
- Android Fragment
- Android fragment
- Android Fragment
- Android Fragment
- android Fragment
- android fragment
- fragment android
- android Fragment
- Android Fragment
- android fragment
- Android Fragment
- Android Fragment
- android Fragment
- 黑马程序员_JAVA 学习笔记28 WEB篇15
- ios7版 30天精通iPhone手机编程第27-30天
- USACO 1.2 Dual Palindromes (dualpal)
- 黑马程序员_JAVA 学习笔记29 WEB篇16
- 黑马程序员_JAVA 学习笔记30 WEB篇17
- (Android) Fragment
- jQuery 几种不同的slide滑动banner,包括自动播放和带缩略图
- 黑马程序员_JAVA 学习笔记31 WEB篇18
- mysql中文乱码处理
- linux上如何截图
- JDK 内置工具 jstack,javah,jps,jstat,jmap,jinfo,jconsole,jhat,jdb,jvisualvm 的作用和使用
- Android项目导入零碎问题
- USACO 1.3 Mixing Milk (milk)
- 提示ORA-03113:通信通道的文件结尾解决导致ORA-01033: ORACLE 正在初始化或关闭(flash_recovery_area空间压力)