Fragment的生命周期:
Fragment正常情况下从创建到销毁的生命周期回调:onAttach(依附于宿主activity),onCreate(系统创建Fragment),onCreateView(创建布局文件),onActivityCreated(activity 的onCreate回调后会调用该生命周期方法),onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy(),onDetach()
旋转屏幕时的生命周期:
2.将手机屏幕向上,旋转180度,不会触发任何生命周期。
添加到Activity中:
<fragment
android:id="@+id/one_fragment"
android:name="com.cy.test.fragmentapplication.OneFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);//第一个参数是 ViewGroup,即应该放置片段的位置,由资源 ID 指定,第二个参数是要添加的片段。
fragmentTransaction.commit();
DialogFragment:
拓展DialogFragment需要实现onCreateView或者onCreateDialog:
// 实现onCreateView
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.dialog_fragment_test1, container);
return inflate;
}
// 实现onCreateDialog
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = Objects.requireNonNull(getActivity()).getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_fragment_test1, null));
return builder.create();
}
<!-- dialog_fragment_test1的布局文件 -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_marginTop="4dp"
android:text="测试。。。。巴拉巴拉巴拉巴拉巴拉。....巴拉。"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:Fragment使用详解-创新互联
文章URL:https://www.cdcxhl.com/article2/dscsic.html
成都网站建设公司_创新互联,为您提供App设计、动态网站、网页设计公司、定制开发、搜索引擎优化、移动网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联