Android 活动和片段之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25822656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between activity and fragment?
提问by Devrath
As per my research, there is a significant difference in the concept of backstack and how they exist:
根据我的研究,backstack 的概念及其存在方式存在显着差异:
Activity
活动
When an
activity
is placed to thebackstack
ofactivities
the user can navigate back to the previous activity by just pressing theback
button.Activity
can exist independently.
当
activity
被放置到 的backstack
时activities
,用户只需按下back
按钮即可导航回上一个活动。Activity
可以独立存在。
Fragment
分段
When an
fragment
is placed to the activity we have to request the instance to be saved by callingaddToBackstack()
during thefragment
transaction
.Fragment
has to live inside theactivity
当一个
fragment
被放置到活动中时,我们必须通过addToBackstack()
在fragment
transaction
.Fragment
必须住在里面activity
Are there any additional differences?
是否有任何其他差异?
采纳答案by Юр?й Мазуревич
Those are two completely different things:
这是两个完全不同的东西:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Activity 是一个应用程序组件,它提供一个屏幕,用户可以与之交互以执行某些操作。更多详情:http: //developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html
而 Fragment 代表 Activity 中的一种行为或用户界面的一部分。 http://developer.android.com/guide/components/fragments.html
回答by siddhartha shankar
Main Differences between Activity and Fragment
Activity 和 Fragment 的主要区别
- Activity is an application component that gives a user interface where the user can interact. The fragment is a part of an activity, which contributes its own UI to that activity.
- For Tablet or if mobile is in landscape then Using fragment we can show two lists like the only list for show the state name and other lists will show the state description in single activity but using Activity we can't do the same thing.
- Activity is not dependent on fragment.but The fragment is dependent on Activity, it can't exist independently.
- without using fragment in Activity we can't create multi-pane UI. but using multiple fragments in a single activity we can create multi-pane UI.
- If we create a project using only Activity then its difficult to manage but if we use fragments then the project structure will be good and we can handle it easily.
- An activity may contain 0 or multiple numbers of fragments. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.
- The activity has own life cycle but fragment has there own life cycle.
- For Activity, we just need to mention in Manifest but for fragment its not required.
- Activity a lot of memory used and the fragment is non-memory used.
- Activity is not lite weight. The fragment is the lite weight.
- Activity 是一个应用程序组件,它提供了一个用户可以交互的用户界面。片段是活动的一部分,它为该活动贡献自己的 UI。
- 对于平板电脑或如果移动设备处于横向状态,那么使用片段我们可以显示两个列表,例如显示状态名称的唯一列表,其他列表将显示单个活动中的状态描述,但使用活动我们不能做同样的事情。
- Activity不依赖于fragment。但是fragment依赖于Activity,它不能独立存在。
- 如果不在 Activity 中使用片段,我们就无法创建多窗格 UI。但是在单个活动中使用多个片段我们可以创建多窗格 UI。
- 如果我们只使用 Activity 创建一个项目,那么它很难管理,但是如果我们使用片段,那么项目结构就会很好,我们可以轻松处理它。
- 一个活动可能包含 0 个或多个片段。一个片段可以在多个活动中重用,因此它在活动中就像一个可重用的组件。
- 活动有自己的生命周期,但片段有自己的生命周期。
- 对于 Activity,我们只需要在 Manifest 中提及,但对于 Fragment 则不需要。
- Activity 使用了大量内存,而片段是非内存使用的。
- 活动不是轻量级的。片段是轻量级。
回答by Ajay S
As per the android developer documentation, difference between fragment & activity in their life cycle.
根据 android 开发人员文档,片段和活动在其生命周期中的区别。
Doc link http://developer.android.com/guide/components/fragments.html#Lifecycle
文档链接http://developer.android.com/guide/components/fragments.html#Lifecycle
The most significant difference in lifecycle between an activity and a fragment is how one is stored in its respective back stack. An activity is placed into a back stack of activities that's managed by the system when it's stopped, by default (so that the user can navigate back to it with the Back button, as discussed in Tasks and Back Stack). However, a fragment is placed into a back stack managed by the host activity only when you explicitly request that the instance be saved by calling addToBackStack() during a transaction that removes the fragment.
Otherwise, managing the fragment lifecycle is very similar to managing the activity lifecycle. So, the same practices for managing the activity lifecycle also apply to fragments. What you also need to understand, though, is how the life of the activity affects the life of the fragment.
Activity 和 Fragment 之间生命周期最显着的区别在于它们如何存储在各自的后台堆栈中。默认情况下,活动被放置在由系统管理的活动的返回堆栈中(以便用户可以使用返回按钮导航回它,如任务和返回堆栈中所述)。但是,仅当您在删除片段的事务期间通过调用 addToBackStack() 显式请求保存实例时,片段才会被放置到由主机活动管理的返回堆栈中。
否则,管理片段生命周期与管理活动生命周期非常相似。因此,管理活动生命周期的相同做法也适用于片段。但是,您还需要了解活动的生命周期如何影响片段的生命周期。
& for multi pane layouts you have to use fragment
that you can't achieve with activity
.
与多窗格布局,你必须使用fragment
你不能与实现activity
。
回答by Shubham Soni
Activity is the UI of an application through which user can interact and Fragment is the part of the Activity,it is an sub-Activity inside activity which has its own Life Cycle which runs parallel to the Activities Life Cycle.
Activity 是应用程序的 UI,用户可以通过它进行交互,而 Fragment 是 Activity 的一部分,它是 Activity 内部的子 Activity,它有自己的生命周期,与 Activity 生命周期并行运行。
Activity LifeCycle Fragment LifeCycle
onCreate() onAttach()
| |
onStart()______onRestart() onCreate()
| | |
onResume() | onCreateView()
| | |
onPause() | onActivityCreated()
| | |
onStop()__________| onStart()
| |
onDestroy() onResume()
|
onPause()
|
onStop()
|
onDestroyView()
|
onDestroy()
|
onDetach()
回答by argon448
Activity
1. Activities are one of the fundamental building blocks of apps on the Android platform. They serve as the entry point for a user's interaction with an app
and are also central to how a user navigates within an app or between apps
2. Lifecycle methods are hosted by OS.
3. Lifecycle of activity
Activity
1. Activity 是 Android 平台上应用程序的基本构建块之一。它们充当用户与应用程序交互的入口点,也是用户如何在应用程序内或应用程序之间导航的核心
。 2. 生命周期方法由操作系统托管。
3.活动生命周期
Fragments
1. A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running.
2. Lifecycle methods are hosted by are hosted by
hosting activity.
3. Lifecycle of a fragment
片段
1. 片段表示活动中的一种行为或用户界面的一部分。您可以在单个活动中组合多个片段来构建多窗格 UI 并在多个活动中重用一个片段。您可以将片段视为活动的模块化部分,它有自己的生命周期,接收自己的输入事件,并且您可以在活动运行时添加或删除它。
2. 生命周期方法由托管活动托管。
3.一个片段的生命周期