Android Fragment使用

本文介绍了一种使用Fragment管理通话记录(CallLog)和短信(SMS)的方法。通过创建两个Fragment(CallLogFragment和SmsFragment),并在各自的onCreateView方法中加载布局文件来实现界面展示。此外,在onResume和onDestroyView方法中处理了注册和注销BroadcastReceiver的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//创建两个Fragment(CallLogFragment和SmsFragment):

public class CallLogFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            // 充气得到界面
            RootView = inflater.inflate(R.layout.call_log_fragment_layout, null);
            ..........
            }
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().unregisterReceiver(mUpCallLogListReceiver);
    }
    public void onResume() {
        super.onResume();
    }
    }
public class SmsFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            // 充气得到界面
            rootView = inflater.inflate(R.layout.sms_fragment_layout, null);
            ..........
            }
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().unregisterReceiver(mUpSmsThreadListReceiver);
    }
    public void onResume() {
        super.onResume();
    }
    }

//将两个fragment分别添加到FragmentActivity

mFragmentManager = this.getSupportFragmentManager();
    FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();

    mCallLogFragment = new CallLogFragment();
    mFragmentTransaction.replace(R.id.main_fragment_layout,mCallLogFragment, "CallLogFragment");
    mFragmentTransaction.commit();

    mSmsFragment = new SmsFragment();
    mFragmentManager.beginTransaction().replace(R.id.main_fragment_layout, mSmsFragment, "SmsFragment");
    mFragmentTransaction.commit().commit();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值