4.15

所花时间(包括上课):1

打码量(行):120

博客量(篇):1

了解到知识点:学习Fragment生命周期

 

import android.content.Context;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;

 

public class LifecycleFragment extends Fragment {

 

    private static final String TAG = "LifecycleFragment";

 

    @Override

    public void onAttach(@NonNull Context context) {

        super.onAttach(context);

        Log.d(TAG, "onAttach - Fragment 被附加到 Activity");

    }

 

    @Override

    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Log.d(TAG, "onCreate - Fragment 正在创建");

    }

 

    @Nullable

    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        Log.d(TAG, "onCreateView - Fragment 正在创建视图");

        return inflater.inflate(R.layout.fragment_lifecycle, container, false);

    }

 

    @Override

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        Log.d(TAG, "onActivityCreated - Fragment 所在的 Activity 的 onCreate 方法执行完毕");

    }

 

    @Override

    public void onStart() {

        super.onStart();

        Log.d(TAG, "onStart - Fragment 正在启动");

    }

 

    @Override

    public void onResume() {

        super.onResume();

        Log.d(TAG, "onResume - Fragment 获取焦点,可以交互");

    }

 

    @Override

    public void onPause() {

        super.onPause();

        Log.d(TAG, "onPause - Fragment 暂停,失去焦点");

    }

 

    @Override

    public void onStop() {

        super.onStop();

        Log.d(TAG, "onStop - Fragment 不可见");

    }

 

    @Override

    public void onDestroyView() {

        super.onDestroyView();

        Log.d(TAG, "onDestroyView - Fragment 销毁视图");

    }

 

    @Override

    public void onDestroy() {

        super.onDestroy();

        Log.d(TAG, "onDestroy - Fragment 销毁");

    }

 

    @Override

    public void onDetach() {

        super.onDetach();

        Log.d(TAG, "onDetach - Fragment 从 Activity 中分离");

    }

}

<!-- fragment_lifecycle.xml -->

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- 可以在这里添加实际的视图内容 -->

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Fragment 生命周期示例"

        android:layout_gravity="center" />

</FrameLayout>

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentTransaction;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 获取 FragmentManager

        FragmentManager fragmentManager = getSupportFragmentManager();

 

        // 开始一个 Fragment 事务

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

 

        // 创建并添加 LifecycleFragment

        LifecycleFragment lifecycleFragment = new LifecycleFragment();

        fragmentTransaction.add(R.id.fragment_container, lifecycleFragment);

 

        // 提交事务

        fragmentTransaction.commit();

    }

}

<!-- activity_main.xml -->

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- Fragment 容器 -->

    <FrameLayout

        android:id="@+id/fragment_container"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</RelativeLayout>

posted @ 2024-04-15 20:25  赵千万  阅读(8)  评论(0编辑  收藏  举报