相關資料:
相關視頻:
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
標籤:其他
