fragment 讲解1

ViewPager中的Fragment之间通信 在mainactivity布局中添加viewpager <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http:... 阅读详情

fragment是什么? 

Android是在Android 3.0 (API level 11)开始引入Fragment的。可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。可以把Fragment设计成可以在多个Activity中复用的模块。


fragment的生命周期:




To create a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class has code that looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate(), onStart(), onPause(), and onStop(). In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.

要创建一个fragment,必须创建一个fragment的子类(或是继承自它的子类)。fragment类的代码看起来很像activity。它与activity一样都有回调函数,例如onCreate(),onStart(),onPause(),和onStop()。事实上,如果你正在将一个现成的Android应用转而使用Fragment来实现,可以简单的将代码从activity的回调函数移植到各自的fragment回调函数中。

Usually, you should implement at least the following lifecycle methods:
一般情况下,你至少需要实现以下几个生命周期方法:
onCreate() 
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
在创建fragment时系统会调用此方法。在实现代码中,你可以初始化想要在fragment中保持的那些必要组件,当fragment处于暂停或者停止状态之后可重新启用它们。


onCreateView() 
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI. 
在第一次为fragment绘制用户界面时系统会调用此方法。为fragment绘制用户界面,这个函数必须要返回所绘出的fragment的根View。如果fragment没有用户界面可以返回空。


onPause() 
The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). 
系统回调用该函数作为用户离开fragment的第一个预兆(尽管这并不总意味着fragment被销毁)。在当前用户会话结束之前,通常要在这里提交任何应该持久化的变化(因为用户可能不再返回)。


Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods are discussed more later, in the section about Handling the Fragment Lifecycle.
大部分应用程序都应该至少为每个fragment实现这三个方法,但是还有许多其他用以操纵fragment生命周期中各个阶段的回调函数。所有生命周期中的回调函数在操纵fragment生命周期一节中稍后再做讨论。



To create a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class has code that looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate(), onStart(), onPause(), and onStop(). In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.


要创建一个fragment,必须创建一个fragment的子类(或是继承自它的子类)。fragment类的代码看起来很像activity。它与activity一样都有回调函数,例如onCreate(),onStart(),onPause(),和onStop()。事实上,如果你正在将一个现成的Android应用转而使用Fragment来实现,可以简单的将代码从activity的回调函数移植到各自的fragment回调函数中。


Usually, you should implement at least the following lifecycle methods:
一般情况下,你至少需要实现以下几个生命周期方法:
onCreate() 
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
在创建fragment时系统会调用此方法。在实现代码中,你可以初始化想要在fragment中保持的那些必要组件,当fragment处于暂停或者停止状态之后可重新启用它们。


onCreateView() 
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI. 
在第一次为fragment绘制用户界面时系统会调用此方法。为fragment绘制用户界面,这个函数必须要返回所绘出的fragment的根View。如果fragment没有用户界面可以返回空。


onPause() 
The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). 
系统回调用该函数作为用户离开fragment的第一个预兆(尽管这并不总意味着fragment被销毁)。在当前用户会话结束之前,通常要在这里提交任何应该持久化的变化(因为用户可能不再返回)。


Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods are discussed more later, in the section about Handling the Fragment Lifecycle.
大部分应用程序都应该至少为每个fragment实现这三个方法,但是还有许多其他用以操纵fragment生命周期中各个阶段的回调函数。所有生命周期中的回调函数在操纵fragment生命周期一节中稍后再做讨论。

To create a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class has code that looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate(), onStart(), onPause(), and onStop(). In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.


要创建一个fragment,必须创建一个fragment的子类(或是继承自它的子类)。fragment类的代码看起来很像activity。它与activity一样都有回调函数,例如onCreate(),onStart(),onPause(),和onStop()。事实上,如果你正在将一个现成的Android应用转而使用Fragment来实现,可以简单的将代码从activity的回调函数移植到各自的fragment回调函数中。


Usually, you should implement at least the following lifecycle methods:
一般情况下,你至少需要实现以下几个生命周期方法:
onCreate() 
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
在创建fragment时系统会调用此方法。在实现代码中,你可以初始化想要在fragment中保持的那些必要组件,当fragment处于暂停或者停止状态之后可重新启用它们。


onCreateView() 
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI. 
在第一次为fragment绘制用户界面时系统会调用此方法。为fragment绘制用户界面,这个函数必须要返回所绘出的fragment的根View。如果fragment没有用户界面可以返回空。


onPause() 
The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). 
系统回调用该函数作为用户离开fragment的第一个预兆(这并不总意味着fragment被销毁)。在当前用户会话结束之前,通常要在这里提交任何应该持久化的变化(因为用户可能不再返回)。

Android开发--fragment中Button点击切换Activity 在Android studio中对Fragment创建Button点击事件进行Activity切换 阅读详情

相关推荐

光耦CTR参数实战:如何避免误触发与功耗陷阱(附计算公式)

本文深入解析光耦CTR参数在电路设计中的关键作用,探讨如何平衡误触发风险与功耗表现。通过TLP521、PC817等常见型号的实测数据对比,提供不同工作状态下的选型策略和计算公式,帮助工程师优化设计,提升系统稳定性和能效。

2a4s6d8f0g的博客 323

fragment简介

Fragment 版本 修改日期 作者 修改内容 V1.0 2021.08.06 李合意 初版 文章目录Fragment1、什么是Fragment2、为什么要有Fragment3、Fragment有什么优点4、Fragment的生命周期5、Fragment使用的一些方法**1.将 fragment添加到Activity的两种方式**a.静态注册:以标签的形式添加到Activity的布局当中。b.动态注册:通过java代码将fragment添加到已存在的宿主Activity中**2.Fra

云儿的博客 1万+

STM32CubeIDE无法连接STLinkV2的一个可能原因

前些日子买了个新的STLINKV2 , ( 老的被我家猫猫一泡尿送上西天了)刚开始还挺正常的, 后来有一天就突然间发现无法调试了.网上尝试了各种方法, 各种操作. 百度了好久. 最后好在我自己研究出来了.我使用的是 STM32CubeIDE 这个开发工具调试选项如下图其它的不需要修改, 然后点击下面的apply 按钮, 然后调试Debug, 提示 No STLink 如下图后来尝试了下载ST-LINK Utility 用它是可以正常连接STLink的, 然后也能正常进行升级操作.

走错路的程序员 9829

Fragment技术

一、什么是FragmentAndroid在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计。平板电脑的屏幕要比手机的大得多,有更多的空间来放更多的UI组件,并且这些组件之间会产生更多的交互。Fragment允许这样的一种设计,而不需要你亲自来管理 viewhierarchy的复杂变化。 通过将activity的...

weixin_34362790的博客 209

大模型训练(1):流水线并行

Gpipe的方法是,在训练时计算和运用的是micro-batch里的均值和方差,但同时持续追踪全部mini-batch的移动平均和方差,以便在测试阶段进行使用。流水线并行的核心思想是:在模型并行的基础上,进一步引入数据并行的办法,即把原先的数据再划分成若干个更小的batch,分批送入GPU进行训练,算一部分就让下一个GPU动起来。从实验结果可知,在关掉NVlinks的情况下,Gpipe一样也能实现随着GPU数量的增加,训练速度也增加的效果。前文说过,随着模型的增加,每块GPU中存储的中间结果也会越大。

呆呆象呆呆的博客 1970

从AT指令到万物互联:移远EG800K-CN模块的协议栈深度解析与实践指南

本文深度解析移远EG800K-CN 4G模块的协议栈架构与AT指令体系,涵盖MQTT云端集成、多协议配置优化及工业级故障排除指南。通过实战示例展示如何利用该模块构建稳定可靠的物联网连接方案,实现从设备到云端的万物互联。

jupyter5notebook的博客 362

Fragment的概述

一、为什么会有Fragment——设计理念 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上–例如平板电脑上,支持更加动态和灵活的UI设计。平板电脑的屏幕要比手机的大得多,有更多的空间来放更多的UI组件,并且这些组件之间会产生更多的交互。Fragment允许这样的一种设计,而不需要你亲自来管理 viewhierarchy的复杂变化。 通过将activity的布局分

fjnu_se的博客 497

图像特征(一)——颜色特征(颜色直方图,颜色矩,颜色集,颜色聚合向量和颜色相关图)

本文主要是为了更好地理解图像特征,理解各方法的原理。转载请注明出处https://mp.youkuaiyun.com/console/editor/html/105090183 图像模式识别的一般步骤为:输入图像--图像预处理--特征提取--特征选择--图像分类--输出结果。 图像特征主要有:颜色特征、形状特征、纹理特征和空间关系特征。 本文主要介绍颜色特征,并给出matlab上实现的demo。 颜...

gloria_iris的博客 3万+

被Cloudflare 5秒盾虐了半个月,终用Python破局:UA+TLS指纹伪装实战,拦截率从42%干到3%

其实5秒盾不难破,难的是摸透它的检测逻辑——它不是“一刀切”地拦截爬虫,而是通过“多维度指纹校验”,判断你是不是“真实用户”。表层:UA和HTTP头要完整、匹配;中层:TLS指纹要和真实浏览器一致(核心);深层:行为要符合人类习惯(请求间隔、JS执行)。这套方案不仅适用于Cloudflare 5秒盾,很多基于指纹检测的反爬系统(如Akamai、Imperva)也适用。2025年的爬虫,早已不是“能爬就行”,而是“爬得稳、不被封”——指纹伪装就是实现这一点的关键。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1849

基于STM32的智能婴儿床控制系统设计(手机APP+蓝牙无线控制)(210)

项目充分运用先进的嵌入式技术和物联网理念,打造出一款集安全性、便利性、舒适性于一体的智能婴儿床控制系统,旨在减轻育儿负担,提高育儿效率,保障婴幼儿健康成长,满足现代家庭对高品质育儿生活的追求。

3478

史上最全Fragment介绍,包括fragment的定义,生命周期,用法

一、为什么要使用Fragment 1、当我们需要动态的多界面切换的时候,就需要将UI元素和Activity融合成一个模块。在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个界面动态改变。在4.0或以上系统中就可以使用新的特性来方便的达到这个效果--Fragment类。Fragment类似一个嵌套Activity,可以定义自己的layout和自己的生命周期。 2、

想飞的企鹅 5987

19. SAP UI5 应用开发教程之十九 - SAP UI5 数据类型和复杂的数据绑定

通过本教程前面的步骤SAP UI5 初学者教程之十七 - 聚合绑定在 UI5 复合控件中的使用的介绍,我们已经学会了如何在 SAP UI5 里显示列表,如下图所示: 本步骤我们给该列表添加更多的业务数据,比如每张发票的金额。 按照本文的步骤完成的 SAP UI5 应用,列表如下图所示: 可以看到,同前一个步骤相比,现在 Invoices 表格里,每张发票的金额和货币单位已经显示出来了。 为了实现这个效果,本步骤的源代码位于我的 Github 这个文件夹下面: https://github.com/wan

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 3692

从0开始讲解Fragment和ViewPager2的结合使用(1):Fragment

一.Fragment 1.什么是Fragment、 ①具备生命周期。使得它类似于一个子Activity,具有一定的独立性。 ②必须依托在Activity中才能运行。Fragment的生命周期也受Activity管理 2.Fragment简单使用 按如图所示操作创建一个空白Fragment 删除暂时不相关的代码,如图所示(onCreate也可以不要) 然后修改创建Fragment的时候自动生成的XML文件 这里我将布局改成了LinearLayout,然后加了一个Button控件(这一步是非必须的) Fr

Android小白 1558

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

android的fragment讲解(1),android开发实习面试题

它提供一些方法来管理一个list view, 例如 onListItemClick()回调来处理点击事件.显示一个 Preference对象的层次结构的列表, 类似于PreferenceActivity.这在为你的应用创建一个"设置"activity时有用处.

2401_83620690的博客 702

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

2024年最全android的fragment讲解(1),2024年最新如何吊打面试官

由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!由于整个文档比较全面,内容比较多,篇幅不允许,下面以截图方式展示。网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。需要这份系统化学习资料的朋友,可以戳这里获取一个人可以走的很快,但一群人才能走的更远!

2401_84537603的博客 945

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Android例子—Fragment实例讲解—底部导航栏的实现(方法1)

1.要实现的效果图以及工程目录结构: 先看看效果图吧: 接着看看我们的工程的目录结构: 2.实现流程: Step 1:写下底部选项的一些资源文件 我们从图上可以看到,我们底部的每一项点击的时候都有不同的效果是吧! 我们是通过是否selected来判定的!我们要写的资源文件有:首先是图片,然后是文字,接着是背景! 图片Drawabl

此博客已停用,博文会逐渐转移到新地址,http://blog.youkuaiyun.com/liupeifeng3514 2059

android fragment参数,fragment之间参数传入和传出

参数传入使用setArgment(bundle),参数回传使用回调方法 mainActivity主布局xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/main_layout"&g...

weixin_39738380的博客 367

android中viewpager+fragment,Android中ViewPager+Fragment的基本使用

这几天学习了一下ViewPager+Fragement的基本使用方法并写了个Demo。现将代码和效果图放上。首先是布局文件xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainAc...

weixin_39853210的博客 1066

通过接口给fragment传值_Android中Activity和Fragment传递数据的两种方式

1、第一种方式,也是最常用的方式,就是使用Bundle来传递参数MyFragment myFragment = new MyFragment();Bundle bundle = new Bundle();bundle.putString("DATA",values);//这里的values就是我们要传的值myFragment.setArguments(bundle);然后在Fragment中的on...

weixin_32248781的博客 755

Android之Fragment(三):动态替换碎片

先动态加载了Fragment1。 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fragment

李俊的博客 1508

Android Developers:创建Fragment

你可以认为Fragment作为Activity的一个模块部分,有它自己的生命周期,获取它自己的事件,并且你可以在Activity运行的时候添加或者移除它(有点像你可以在不同的Activity中重用的一个”子Activity“)。这节课程讲述如何使用Support Library继承Fragment类,所以你的应用程序仍然是兼容运行的系统版本低于Android1.6的设备。  注意

1024工场 3558

activity 点击后传递数据给fragment_Fragment 新特性 : Fragment Result API 使用以及源码分析

原标题: Android Fragments: Fragment Result原文地址: https://proandroiddev.com/android-fragments-fragment-result......原文作者: Husayn Hakeem今年 Google 推出了 Fragment Result API 和 Activity Results API,用来取代之前的 Activi...

weixin_39562615的博客 482

在ViewPager中实现两个fragment之间点击跳转与几种数据传递的方法

一、ViewPager+fragment可以实现界面的滑动与点击跳转,结合ViewPager,Fragment之间的滑动很简单,下面首先我们来看看如何实现fragment结合ViewPager的使用:图片效果这是程序主界面向左滑动会滑到第二个fragment上这里写代码片:这是MainActivity布局,很简单 <?xml version="1.0" encoding="utf-8"?> <Lin

liniu5136的博客 2942

Drawer Layout 与 Fragment的组合使用

现在Drawer Layout 与 Fragment在很多APP中都有出现,所以写篇博客,zon自己

francisshi的专栏 2057

Fragment间共享ViewModel

需要使用: val viewModel = ViewModelProvider(requireActivity()).get(TestViewModel::class.java) ViewModelProvider构造方法传入FragmentActivity ViewModelProvider 的 of 方法,他主要有四个方法,分别是 1.ViewModelProvider of(@NonNull Fragment fragment) 2.ViewModelProvider of(@NonNull Fr

Neo的博客 1627
上一篇: PHP如何获得指定日期的上周一是哪一天,有源码
下一篇: 如何清理Chrome浏览器的dns缓存
JadePlus
博客等级 码龄19年 41粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值