主頁 > 移動端開發 > Fragment使用(上)

Fragment使用(上)

2021-04-27 18:01:39 移動端開發

相關資料:

相關視頻:

Fragment應用上

Fragment應用下

相關文章:

Fragment 用法總結(一)

1、Fragment應用

1.1、Fragment靜態應用

1.1.1、代碼實體

實作案例效果:兩個Fragment構成Activity的布局,一個標題Fragment,一個內容Fragment

TitleFragment

/**
 * 創建和使用Fragment的步驟:
 * 1、創建子類繼承Fragment
 * 2、重寫onCreateView()方法,該方法主要定義Fragment的布局,以view物件的方式回傳Fragment的布局
 * 3、將Fragment引入到Activity中
 */
public class TitleFragment extends Fragment {

    @Nullable
    @Override
    @SuppressLint("InflateParams")
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_title, null);
        RelativeLayout rl=view.findViewById(R.id.rl_title);
        rl.setOnClickListener(v ->
                Toast.makeText(getActivity(),"點擊了標題欄",Toast.LENGTH_SHORT).show());
        return view;
    }
}

布局檔案:fragment_title

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_title"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/light_blue_2e">

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:paddingLeft="15dp"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        android:src="@drawable/arrow_back_black"
        />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="我是標題"
        android:textSize="20sp"/>

</RelativeLayout>

ContentFragment

public class ContentFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_content, null);
    }
}

布局檔案:fragment_content

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="我是內容"
        android:textSize="30sp"/>

</RelativeLayout>

Test1FragmentActivity

/**
 * @author songzi522
 *
 * 靜態使用Fragment
 * 步驟:
 * 1、繼承Fragment,重寫onCreateView()回呼方法,設定Fragment的布局
 * 2、在Activity中宣告Fragment,使用方式和View相同
 *
 * 實作案例效果:兩個Fragment構成Activity的布局,一個標題Fragment,一個內容Fragment
 *
 */
public class Test1FragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_test1_fragment);
    }
}

布局檔案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".fragment.Test1FragmentActivity">

    <fragment
        android:id="@+id/fragment_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:name="com.gs.common3.fragment.TitleFragment"
        />

    <fragment
        android:id="@+id/fragment_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/fragment_title"
        android:name="com.gs.common3.fragment.ContentFragment"
        />

</RelativeLayout>

1.1.2、總結

1.2、Fragment動態應用

代碼如下:

/**
 * 演示Fragment的動態使用
 * <p>
 * 案例效果:在Activity界面中有兩個Fragment 標題和內容
 */
public class Test2FragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2_fragment);
        //1.創建Fragment的管理器物件;
        FragmentManager manager = getFragmentManager();
        //2.獲取Fragment的事務物件并開啟事務;
        FragmentTransaction transaction = manager.beginTransaction();
        //3.呼叫事務中相應的動態操作Fragment的方法執行;
        transaction.add(R.id.title_layout, new TitleFragment());
        transaction.add(R.id.content_layout, new ContentFragment());
        //4.提交事務;
        transaction.commit();
    }

}

布局檔案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".fragment.Test2FragmentActivity">

    <LinearLayout
        android:id="@+id/title_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="vertical">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>

</RelativeLayout>

2、Fragment動態切換

ShopRankFragment

public class ShopRankFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shop_rank,null);
    }
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/home_shop"
        android:textSize="30sp"/>

</RelativeLayout>

ShareFragment

public class ShareFragment extends Fragment {
    @SuppressLint("InflateParams")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_share, null);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/home_share"
        android:textSize="30sp"/>

</RelativeLayout>

GiftFragment

public class GiftFragment extends Fragment {
    @SuppressLint("InflateParams")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_gift, null);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/home_gift"
        android:textSize="30sp"/>

</RelativeLayout>

OrderFragment

public class OrderFragment extends Fragment {
    @SuppressLint("InflateParams")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_order, null);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/home_order"
        android:textSize="30sp"/>

</RelativeLayout>

Test3FragmentActivity

public class Test3FragmentActivity extends AppCompatActivity implements View.OnClickListener {

    private FragmentManager manager;
    private FragmentTransaction transaction;

    @SuppressLint("CommitTransaction")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test3_fragment);

        initViews();

        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.content_layout, new ShopRankFragment());
        transaction.commit();
    }

    private void initViews() {
        RadioButton rbShopRank = findViewById(R.id.rb_shop_rank);
        RadioButton rbShare = findViewById(R.id.rb_share);
        RadioButton rbGift = findViewById(R.id.rb_gift);
        RadioButton rbOrder = findViewById(R.id.rb_order);

        rbShopRank.setOnClickListener(this);
        rbShare.setOnClickListener(this);
        rbGift.setOnClickListener(this);
        rbOrder.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        transaction = manager.beginTransaction();
        switch (v.getId()) {
            case R.id.rb_shop_rank:
                transaction.replace(R.id.content_layout,new ShopRankFragment());
                break;
            case R.id.rb_share:
                transaction.replace(R.id.content_layout,new ShareFragment());
                break;
            case R.id.rb_gift:
                transaction.replace(R.id.content_layout,new GiftFragment());
                break;
            case R.id.rb_order:
                transaction.replace(R.id.content_layout,new OrderFragment());
                break;
            default:
                break;
        }
        transaction.commit();
    }
}

布局檔案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".fragment.Test3FragmentActivity">

    <!-- 展示內容界面-->
    <LinearLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

    <!-- 展示切換標簽-->
    <LinearLayout
        android:id="@+id/bottom_layout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="#ffffff"
        android:layout_alignParentBottom="true">

        <RadioGroup
            android:id="@+id/rg_home"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_shop_rank"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:button="@null"
                android:drawableTop="@mipmap/ic_launcher"
                android:drawablePadding="10dp"
                android:gravity="center"
                android:text="@string/home_shop"
                android:textColor="#B3B3B3"
                android:textSize="15sp" />

            <RadioButton
                android:id="@+id/rb_share"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:button="@null"
                android:drawableTop="@mipmap/ic_launcher"
                android:drawablePadding="10dp"
                android:gravity="center"
                android:text="@string/home_share"
                android:textColor="#B3B3B3"
                android:textSize="15sp" />

            <RadioButton
                android:id="@+id/rb_gift"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:button="@null"
                android:drawableTop="@mipmap/ic_launcher"
                android:drawablePadding="10dp"
                android:gravity="center"
                android:text="@string/home_gift"
                android:textColor="#B3B3B3"
                android:textSize="15sp" />

            <RadioButton
                android:id="@+id/rb_order"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:button="@null"
                android:drawableTop="@mipmap/ic_launcher"
                android:drawablePadding="10dp"
                android:gravity="center"
                android:text="@string/home_order"
                android:textColor="#B3B3B3"
                android:textSize="15sp" />

        </RadioGroup>


    </LinearLayout>

</RelativeLayout>

3、Fragment和Activity的生命周期關聯對比

3.1、代碼實體:

3.1.1、Test4FragmentActivity

public class Test4FragmentActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "Test_LifeCycle";

    private FragmentManager manager;
    private FragmentTransaction transaction;

    @SuppressLint("CommitTransaction")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test4_fragment);

        Log.i(TAG, "-----Test4FragmentActivity-----onCreate-----");

        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.content_layout, new HotSpotFragment());
        transaction.commit();

        findViewById(R.id.tv_hot_spot).setOnClickListener(this);
        findViewById(R.id.tv_top_line).setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        transaction = manager.beginTransaction();
        switch (v.getId()) {
            case R.id.tv_hot_spot:
                transaction.replace(R.id.content_layout, new HotSpotFragment());
                break;
            case R.id.tv_top_line:
                transaction.replace(R.id.content_layout, new TopLineFragment());
                break;
            default:
                break;
        }
        transaction.commit();
    }

    /**
     * Activity能夠被用戶看到時呼叫
     */
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "-----Test4FragmentActivity-----onStart-----");
    }

    /**
     * Activity能夠獲取用戶焦點時呼叫
     */
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "-----Test4FragmentActivity-----onResume-----");
    }

    /**
     * Activity失去用戶焦點時呼叫
     */
    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "-----Test4FragmentActivity-----onPause-----");
    }

    /**
     * Activity完全被遮擋時呼叫
     */
    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "-----Test4FragmentActivity-----onStop-----");
    }

    /**
     * Activity處于停止狀態,重新被用戶使用 時呼叫
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "-----Test4FragmentActivity-----onRestart-----");
    }

    /**
     * Activity被銷毀 時呼叫
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "-----Test4FragmentActivity-----onDestroy-----");
    }


}

布局檔案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.test4.Test4FragmentActivity">

    <LinearLayout
        android:id="@+id/top_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_hot_spot"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/hot_spot"
            android:gravity="center"
            android:textSize="30sp"/>

        <TextView
            android:id="@+id/tv_top_line"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/top_line"
            android:gravity="center"
            android:textSize="30sp"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/top_layout"/>

</RelativeLayout>

3.1.2、HotSpotFragment

public class HotSpotFragment extends Fragment {

    private static final String TAG = "Test_LifeCycle";

    /**
     * 表示 activity 和 fragment 產生關聯時回呼的方法
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.i(TAG, "-----HotSpotFragment-----onAttach-----");
    }

    /**
     * 表示當 fragment 第一次被創建時回呼的方法
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "-----HotSpotFragment-----onCreate-----");
    }

    /**
     * 表示當 fragment 第一次繪制用戶界面回呼的方法
     */
    @SuppressLint("InflateParams")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Log.i(TAG, "-----HotSpotFragment-----onCreateView-----");
        return inflater.inflate(R.layout.fragment_hot_spot, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "-----HotSpotFragment-----onViewCreated-----");
    }

    /**
     * 表示當前 fragment 所屬activity創建成功時回呼的方法
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "-----HotSpotFragment-----onActivityCreated-----");
    }

    /**
     * 表示當前 fragment 能夠被用戶看到時回呼的方法,略晚于所屬activity的 onStart方法
     */
    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "-----HotSpotFragment-----onStart-----");
    }

    /**
     * 表示當前 fragment 獲取到用戶焦點時回呼的方法,略晚于所屬activity的onResume方法
     */
    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "-----HotSpotFragment-----onResume-----");
    }

    /**
     * 表示當前 fragment 失去用戶焦點時 回呼的方法,略早于所屬activity的 onPause 方法
     */
    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "-----HotSpotFragment-----onPause-----");
    }

    /**
     * 表示當前 fragment 被完全遮擋時 回呼的方法,略早于所屬activity的 onStop 方法
     */
    @Override
    public void onStop() {
        super.onStop();
        Log.i(TAG, "-----HotSpotFragment-----onStop-----");
    }

    /**
     * 表示activity中的fragment的視圖被移除時回呼的方法
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "-----HotSpotFragment-----onDestroyView-----");
    }

    /**
     * 表示fragment被銷毀時回呼的方法
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "-----HotSpotFragment-----onDestroy-----");
    }

    /**
     * 表示activity與fragment失去關聯時回呼的方法
     */
    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(TAG, "-----HotSpotFragment-----onDetach-----");
    }
}

布局檔案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/hot_spot"
        android:textColor="#00aa00"
        android:textSize="30sp"/>

</RelativeLayout>

3.1.3、TopLineFragment

public class TopLineFragment extends Fragment {

    @SuppressLint("InflateParams")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_top_line,null);
    }

}

布局檔案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/top_line"
        android:textColor="#aa0000"
        android:textSize="30sp"/>

</RelativeLayout>

3.2、結合具體操作觀察生命周期

3.2.1、啟動專案

日志列印:

2021-04-26 16:20:40.081 3179-3179/com.gs.common3 I/Test_LifeCycle: -----Test4FragmentActivity-----onCreate-----
2021-04-26 16:20:40.082 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onAttach-----
2021-04-26 16:20:40.082 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onCreate-----
2021-04-26 16:20:40.082 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onCreateView-----
2021-04-26 16:20:40.083 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onViewCreated-----
2021-04-26 16:20:40.083 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onActivityCreated-----
2021-04-26 16:20:40.085 3179-3179/com.gs.common3 I/Test_LifeCycle: -----Test4FragmentActivity-----onStart-----
2021-04-26 16:20:40.085 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onStart-----
2021-04-26 16:20:40.086 3179-3179/com.gs.common3 I/Test_LifeCycle: -----Test4FragmentActivity-----onResume-----
2021-04-26 16:20:40.086 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onResume-----

3.2.2、關掉螢屏,再打開

關掉螢屏:

2021-04-26 16:25:03.957 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onPause-----
2021-04-26 16:25:03.958 3179-3179/com.gs.common3 I/Test_LifeCycle: -----Test4FragmentActivity-----onPause-----
2021-04-26 16:25:04.006 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onStop-----
2021-04-26 16:25:04.007 3179-3179/com.gs.common3 I/Test_LifeCycle: -----Test4FragmentActivity-----onStop-----

打開螢屏:

2021-04-26 16:25:29.516 3179-3179/com.gs.common3 I/Test_LifeCycle: -----Test4FragmentActivity-----onRestart-----
2021-04-26 16:25:29.525 3179-3179/com.gs.common3 I/Test_LifeCycle: -----Test4FragmentActivity-----onStart-----
2021-04-26 16:25:29.525 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onStart-----
2021-04-26 16:25:29.528 3179-3179/com.gs.common3 I/Test_LifeCycle: -----Test4FragmentActivity-----onResume-----
2021-04-26 16:25:29.529 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onResume-----

3.2.3、切換到別的fragment

2021-04-26 16:27:48.741 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onPause-----
2021-04-26 16:27:48.741 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onStop-----
2021-04-26 16:27:48.741 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onDestroyView-----
2021-04-26 16:27:48.742 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onDestroy-----
2021-04-26 16:27:48.742 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onDetach-----

再切回來:

2021-04-26 16:30:24.675 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onAttach-----
2021-04-26 16:30:24.675 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onCreate-----
2021-04-26 16:30:24.675 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onCreateView-----
2021-04-26 16:30:24.683 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onViewCreated-----
2021-04-26 16:30:24.683 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onActivityCreated-----
2021-04-26 16:30:24.683 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onStart-----
2021-04-26 16:30:24.684 3179-3179/com.gs.common3 I/Test_LifeCycle: -----HotSpotFragment-----onResume-----

4、總結

是不是能發現Fragment和Activity的生命周期太相似了,現在只需要再介紹幾個Activity中沒講過的新方法:

onAttach():當Fragment和Activity建立關聯時呼叫
onCreateView():當Fragment創建視圖時呼叫
onActivityCreated():當與Fragment相關聯的Activity完成onCreate()之后呼叫
onDestroyView():在Fragment中的布局被移除時呼叫
onDetach():當Fragment和Activity解除關聯時呼叫

activity生命周期方法有7個,fragment有11個,多了onAttach、onCreateView、onActivityCreated、onDestroyView、onDestroyView,少了onRestart,

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/280718.html

標籤:其他

上一篇:Android連載39-簡析HAL、撥號盤

下一篇:mac 配置Clion運行C和C++的環境

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【從零開始擼一個App】Dagger2

    Dagger2是一個IOC框架,一般用于Android平臺,第一次接觸的朋友,一定會被搞得暈頭轉向。它延續了Java平臺Spring框架代碼碎片化,注解滿天飛的傳統。嘗試將各處代碼片段串聯起來,理清思緒,真不是件容易的事。更不用說還有各版本細微的差別。 與Spring不同的是,Spring是通過反射 ......

    uj5u.com 2020-09-10 06:57:59 more
  • Flutter Weekly Issue 66

    新聞 Flutter 季度調研結果分享 教程 Flutter+FaaS一體化任務編排的思考與設計 詳解Dart中如何通過注解生成代碼 GitHub 用對了嗎?Flutter 團隊分享如何管理大型開源專案 插件 flutter-bubble-tab-indicator A Flutter librar ......

    uj5u.com 2020-09-10 06:58:52 more
  • Proguard 常用規則

    介紹 Proguard 入口,如何查看輸出,如何使用 keep 設定入口以及使用實體,如何配置壓縮,混淆,校驗等規則。

    ......

    uj5u.com 2020-09-10 06:59:00 more
  • Android 開發技術周報 Issue#292

    新聞 Android即將獲得類AirDrop功能:可向附近設備快速分享檔案 谷歌為安卓檔案管理應用引入可安全隱藏資料的Safe Folder功能 Android TV新主界面將顯示電影、電視節目和應用推薦內容 泄露的Android檔案暗示了傳說中的谷歌Pixel 5a與折疊屏新機 谷歌發布Andro ......

    uj5u.com 2020-09-10 07:00:37 more
  • AutoFitTextureView Error inflating class

    報錯: Binary XML file line #0: Binary XML file line #0: Error inflating class xxx.AutoFitTextureView 解決: <com.example.testy2.AutoFitTextureView android: ......

    uj5u.com 2020-09-10 07:00:41 more
  • 根據Uri,Cursor沒有獲取到對應的屬性

    Android: 背景:呼叫攝像頭,拍攝視頻,指定保存的地址,但是回傳的Cursor檔案,只有名稱和大小的屬性,沒有其他諸如時長,連ID屬性都沒有 使用 cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATIO ......

    uj5u.com 2020-09-10 07:00:44 more
  • Android連載29-持久化技術

    一、持久化技術 我們平時所使用的APP產生的資料,在記憶體中都是瞬時的,會隨著斷電、關機等丟失資料,因此android系統采用了持久化技術,用于存盤這些“瞬時”資料 持久化技術包括:檔案存盤、SharedPreference存盤以及資料庫存盤,還有更復雜的SD卡記憶體儲。 二、檔案存盤 最基本存盤方式, ......

    uj5u.com 2020-09-10 07:00:47 more
  • Android Camera2Video整合到自己專案里

    背景: Android專案里呼叫攝像頭拍攝視頻,原本使用的 MediaStore.ACTION_VIDEO_CAPTURE, 后來因專案需要,改成了camera2 1.Camera2Video 官方demo有點問題,下載后,不能直接整合到專案 問題1.多次拍攝視頻崩潰 問題2.雙擊record按鈕, ......

    uj5u.com 2020-09-10 07:00:50 more
  • Android 開發技術周報 Issue#293

    新聞 谷歌為Android TV開發者提供多種新功能 Android 11將自動填表功能整合到鍵盤輸入建議中 谷歌宣布Android Auto即將支持更多的導航和數字停車應用 谷歌Pixel 5只有XL版本 搭載驍龍765G且將比Pixel 4更便宜 [圖]Wear OS將迎來重磅更新:應用啟動時間 ......

    uj5u.com 2020-09-10 07:01:38 more
  • 海豚星空掃碼投屏 Android 接收端 SDK 集成 六步驟

    掃碼投屏,開放網路,獨占設備,不需要額外下載軟體,微信掃碼,發現設備。支持標準DLNA協議,支持倍速播放。視頻,音頻,圖片投屏。好點意思。還支持自定義基于 DLNA 擴展的操作動作。好像要收費,沒體驗。 這里簡單記錄一下集成程序。 一 跟目錄的build.gradle添加私有mevan倉庫 mave ......

    uj5u.com 2020-09-10 07:01:43 more
最新发布
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:40:31 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:40:11 more
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:39:36 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:39:13 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:16:23 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:16:15 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:15:46 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:14:53 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:14:08 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:08:34 more