Android学习14
Fragment
- Fragment是依赖于Activity的,不能独立存在的。
一个Activity里可以有多个Fragment。
一个Fragment可以被多个Activity重用。
Fragment有自己的生命周期,并能接收输入事件。
- 我们能在Activity运行时动态地添加或删除Fragment。
Fragment的生命周期
Fragment必须是依存于Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。

- onAttach(Activity); //当Activity与Fragment发生关联时调用
- onCreateView(LayoutInflater,ViewGroup,Bundle); //创建该Fragment的视图
- onActivityCreate(bundle); //当Activity的onCreate();方法返回时调用
- onDestoryView(); //与onCreateView相对应,当改Fragment被移除时调用
- onDetach(); //与onAttach()相对应,当Fragment与Activity的关联被取消时调用
Container.java:
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout; import com.example.helloworld.R; public class ContainerActivity extends AppCompatActivity { private AFragment aFragment;
private BFragment bFragment;
private Button mBtnChange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
mBtnChange = findViewById(R.id.btn_change);
mBtnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bFragment == null){
bFragment = new BFragment();
}
getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).commitAllowingStateLoss();
}
}); //实例化AFragment
aFragment = new AFragment();
//把AFragment添加到Activity中
getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment).commitAllowingStateLoss();
}
}
activity_container:
<?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"> <Button
android:id="@+id/btn_change"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="更换Fragment" /> <FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btn_change"/> </RelativeLayout>
两个Fragment 分别为AFagment和BFragment
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import com.example.helloworld.R; public class AFragment extends Fragment { private TextView mTvTitle;
private Activity mActivity; @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a,container,false);
return view;
} @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); mTvTitle = view.findViewById(R.id.tv_title);
} }
点击按钮 切换Fragment

Android学习14的更多相关文章
- 【Android学习】《Android开发视频教程》第一季笔记
视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5 Activity基础概念 1.Android开发技术结构 ...
- Android – 学习操作NFC – 2
在<Android – 学习操作NFC – 1>说明了Android在处理NFC tag的机制.tag dispatch system的运作流程,以及三种ACTION_NDEF_DISCO ...
- 10、android学习资源整理
1.github上整理好的开源工程 https://github.com/Trinea/android-open-project 2.最流行的android组件大全 http://colobu.com ...
- android学习系列:jercy——AI3 的博客
[android学习之十七]——特色功能2:桌面组件(快捷方式,实时文件夹) 二.桌面组件 1.快捷方式 Android手机上得快捷方式的意思可以以我们实际PC机器上程序的快捷方式来理解.而andro ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android学习系列(7)--App轮询服务器消息
这篇文章是android开发人员的必备知识. 1.轮询服务器 一般的应用,定时通知消息可以采用轮询的方法从服务器拿取消息,当然实时消息通知的话,建议采用推送服务. 其中需要注意轮询的频率 ...
- Android学习系列(15)--App列表之游标ListView(索引ListView)
游标ListView,提供索引标签,使用户能够快速定位列表项. 也可以叫索引ListView,有的人称也为Tweaked ListView,可能更形象些吧. 一看图啥都懂了: 1. ...
- Android学习系列(23)--App主界面实现
在上篇文章<Android学习系列(22)--App主界面比较>中我们浅略的分析了几个主界面布局,选了一个最大众化的经典布局.今天我们就这个经典布局,用代码具体的实现它. 1.预览图先看下 ...
- Xamarin.Android学习之应用程序首选项
Xamarin.Android学习之应用程序首选项 一.前言 任何App都会存在设置界面,如果开发者利用普通控件并绑定监听事件保存设置,这一过程会非常的枯燥,而且耗时.我们可以看到Android系统的 ...
随机推荐
- STL标准库面试题(转)
一.vector的底层(存储)机制 二.vector的自增长机制 三.list的底层(存储)机制 四.什么情况下用vector,什么情况下用list 五.list自带排序函数的排序原理 六.deque ...
- Linux修复日志
阿里云后台系统报告漏洞,解决记录 中级: RHSA-2019:0049-重要: systemd 安全更新
- 在npm上如何发布自己的包
环境需要:node和npm 然后在本地建一个文件夹作为你要发布的模块 然后在这个文件夹下打开cmd 执行:npm init 按照提示写对应的,或者先不写也可以,最后y 这时候在mcrm这个文件夹下就出 ...
- linux上部署springboot应用的脚本
#!/bin/bash #getProcessId then kill pids=$(ps -ef | grep flashsale| awk '{print $2}') for pid in $pi ...
- http接口的调用
1.按照文档先写入参数,这里主要介绍 Json格式的String字符串,包括拼接数组 String sqr_arry [] = new String[rowList.size()]; for(int ...
- COS上传图片和显示图片
写这篇文章之前,我也是刚刚实现COS上传和显示图片.我百度了好多相关文章,COS上传图片成功的文章不少,上传后显示图片的文章几乎没有.于是写一篇记录下. COS上传图片推荐链接:https://blo ...
- python面试的100题(20)
76.递归函数停止的条件? 递归的终止条件一般定义在递归函数内部,在递归调用前要做一个条件判断,根据判断的结果选择是继续调用自身,还是return:返回终止递归.终止的条件:1.判断递归的次数是否达到 ...
- 批量启动关闭MS SQL 2005服务BAT
当装上了MSSQL2005后,内存的占用会变得很大.所以如果用一个批量处理来开启或关闭MSSQL2005所有的服务,那将会让我们的电脑更好使用.根据自己的经验,做出了下面两个批处理: 1.开启服务:( ...
- 一些docker资料汇总
安装vmtools https://blog.csdn.net/qq_37764098/article/details/95538813 挂载vm共享文件夹 https://www.cnblogs.c ...
- 《NVMe-over-Fabrics-1_0a-2018.07.23-Ratified》阅读笔记(3)-- 命令
3 命令 Fabrics命令用于创建队列和初始化controller.Fabrics命令的Opcode字段填写0x7F.无论controller是否处于使能状态(CC.EN)Fabrics命令都会被处 ...