0 systematics Android - 4.2 Fragment Lifecycle

0 systematics Android--


This series of articles catalog : More boutique Article Categories

This series of continuously updated .... Primary Stage content reference "first line"

4.3 pieces of the life cycle

Debris also has its own life cycle, and the cycle of life and Activity also like.

State 4.3.1 debris and callbacks

  1. Operating status

    When a fragment is visible, and it is associated with activity is in the operating state, this debris is also running.

  2. Suspended state

    When an Activity to enter a suspended state, visible debris associated with it will enter into a suspended state.

  3. Stop state

    When an activity into a stopped state, the debris associated with him will enter into a stopped state, or by calling FragmentTransactionin remove()or replace()removing debris from the active method, provided that the call before the transaction commits addToBackStack()method, then the debris will enter to stop state.

    // 前提当前的 Activity 处于运行状态。
    Fragment A = new Fragment();
    FragmentManager fragmentManager = getSupportFragmentManger();
    FrgmentTransaction fragmentTransaction = fragmentManger.beginTransaction();
    fragmentTransaction.replace(R.id.framelayout,A);// 这个时候 Fragment A应该是处于运行状态的。
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    Fragment B = new Fragment();
    fragmentTransaction.replace(R.id.framelayout,B);// 这个时候 AFragment 的状态时停止状态的。
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

    Brought to a standstill for the debris is completely invisible to the user, there may be recovery system.

  4. Destruction of state

    Activity is always attached to the debris exist, so when the Activity is destroyed, the debris will enter the associated destruction of the state. Or by calling FragmentTransaction()in reove(), replace()methods removing debris from the Activity, and did not call before the transaction commits addToBackStatck()method, then the amount of debris can enter the destroyed state.

  • onAttach () when debris and Activity associate of use
  • onCreateView () call to create a view (load distribution) into pieces
  • onActivityCreated () it must have been created to ensure that the debris associated with Activity when called.
  • onDestroyView () when associated with the removal of the debris of view is invoked
  • onDetach () when debris and Activity disassociated call.

4.3.2 Experience debris lifecycle

就是在上一个例子的基础上,在每个方法汇总打印一下,观察一下效果就好了。

这里给宿主 FragmentBaseActivity 和 静态添加到 Activity 中的 Fragment 的生命周期方法里面添加打印。效果:

在碎片中,你也是可以通过 onSaveInstanceState() 方法来保存数据,因为进入停止状态的碎片也是有可能被系统在内存不足的情况下收回的。保存下来的数据在 onCreateView()onActivityCreated()onCreate() 中都可以获取到

4.4 动态加载布局的技巧

程序如何能够根据设备的分辨率或屏幕大小在运行时来决定加载哪个布局呢?

4.4.1 使用限定符

经常使用平板电脑我们会发现有些应用都是采用双页模式(程序会在左侧面板上显示一个包含子项的列表,在右侧的面板上显示内容)。这是因为平板电脑屏幕的大的原因,完全可以显示两页的内容。但是手机就不行了,屏幕比较小,只能显示一页的内容。

那么怎么样才能在运行时判断程序应该使用双页模式还是单页模式呢?这就需要限定符(Qualifiers)实现。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  
  android:name="com.example.firstcode.fourth_chapter.LeftFragment"/>
</LinearLayout>

这个时候让它充满了整个页面。

在 res 目录下新建 layout_large 文件夹,在这个文件夹新建一个布局,也叫做 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:name="com.example.firstcode.fourth_chapter.LeftFragment"
    android:id="@+id/fg_left"/>
 <fragment
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:name="com.example.firstcode.fourth_chapter.RightFragment"
     android:id="@+id/fg_right"/>
</LinearLayout>

其中 large 就是一个限定符,那些屏幕被认为是large 的设备就会自动加载 layout-large 文件下的布局,而小屏幕的设备还是会加载 layout 下的布局。

Android 中一些常见的限定符

4.4.2 使用最小宽度限定符

large 到底指多大呢?有时候我们需要更加灵活,不管它们是不是被系统认定为 large 这个时候就可以使用最小宽度限定符(Smallest-width Qualifier)。

最小宽度限定符允许我们指定一个最小的值(以dp为单位),然后如果屏幕大于等于这个最小值则使用这个文件夹内的布局,如果小于则使用默认布局

在 res 目录新建 layout-sw600dp 文件,在这个文件夹创建布局就可以了。

4.5 碎片的实践----简易版新闻应用

首先我们要实现的效果是在普通手机上是单页模式,也就是有一个新闻标题列表,点击后进入下一个页面(新闻详情页面)。

在平板电脑上效果是双页模式:效果如

试想我们是不是可以为手机和平板每个端提供一个应用程序呢?如果这样做的话会浪费很多的人力物力,因为这样意味着要同时维护两份代码。

Fragment 的出现就是为了充分利用屏幕空间的。这里可以使用 Fragment 来很多的处理这个问题。

思路:通过限定符,在手机上面会加载直接标题列表的页面,在平板上会加载列表和内容页面。

其中为了复用,标题列表和内容页面都应该是一个 Fragment。这样就达到了复用的效果。

实现代码:MyGitHub/FirstCode/app/src/main/java/com/example/firstcode/fourth_chapter/news 包下

4.6 总结

Fragment 运用在要求充分利用屏幕资源的情况下

加载方式有静态加载和动态加载。

Guess you like

Origin www.cnblogs.com/sydmobile/p/12103646.html