Fragment使用(上)

相关资料:

相关视频:

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。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值