📜  片段和Android中的活动之间的区别

📅  最后修改于: 2021-05-13 15:16:23             🧑  作者: Mango

活动是一个用户界面组件,主要用于构建应用程序的单个屏幕,并表示关注屏幕的主要焦点。一个活动可以一次托管一个或多个片段。随着平板电脑出现在更大的屏幕上,碎片是可重用的组件,它们附加到活动中并显示在活动中。从根本上讲,它是一项活动,可以实现更多模块化的活动设计。我们可以称片段是一种子活动。它始终由活动托管。它具有自己的布局和行为,并具有自己的生命周期回调。我们可以在活动运行时在活动中添加或删除片段。可以仅使用活动来开发UI,但这通常是一个坏主意,因为它们的代码以后无法在其他活动中重用,并且不能支持多个屏幕。活动是用户可以通过其进行交互的应用程序的UI,而Fragment是活动的一部分,它是活动内部的子活动,它具有自己的生命周期,该生命周期与活动生命周期平行。

活动生命周期

要了解更多信息,请参阅带有演示应用程序的Android中的“活动生命周期”

片段生命周期

要了解更多信息,请参阅Android中的Fragment Lifecycle

差异表

Activity

Fragment

Activity is an application component that gives a user interface where the user can interact.  The fragment is only part of an activity, it basically contributes its UI to that activity.
Activity is not dependent on fragment  Fragment is dependent on activity. It can’t exist independently.
we need to mention all activity it in the manifest.xml file  Fragment is not required to mention in  the manifest file
We can’t create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI.
Activity can exist without a Fragment   Fragment cannot be used without an Activity.
Creating a project using only Activity then it’s difficult to manage While Using fragments in the project, the project structure will be good and we can handle it easily.
Lifecycle methods are hosted by OS. The activity has its own life cycle. Lifecycle methods in fragments are hosted by hosting the activity.
Activity is not lite weight.  The fragment is the lite weight.
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!