活动和片段之间有什么区别?

时间:2021-05-28 09:12:07

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 the backstack of activities the user can navigate back to the previous activity by just pressing the back button.

    当一个活动被放置到活动的后台时,用户只需按下back按钮就可以导航回先前的活动。

  • Activity can exist independently.

    活动可以独立存在。

Fragment::

  • When an fragment is placed to the activity we have to request the instance to be saved by calling addToBackstack() during the fragment transaction.

    当一个片段被放置到活动中时,我们必须通过在片段事务期间调用addToBackstack()来请求要保存的实例。

  • Fragment has to live inside the activity

    碎片必须生活在活动中。

Are there any additional differences ?

还有其他的区别吗?

7 个解决方案

#1


33  

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

活动是提供屏幕的应用程序组件,用户可以使用该组件进行交互以完成某些操作。更多信息: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

而片段表示活动中的行为或用户界面的一部分。http://developer.android.com/guide/components/fragments.html

#2


33  

  1. Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity, where as the complete screen with which user interacts is called as activity. An activity can contain multiple fragments. Fragment are mostly a part of an activity.

    片段是活动的一部分,它为该活动提供自己的UI。片段可以看作子活动,其中作为用户交互的完整屏幕被称为活动。一个活动可以包含多个片段。碎片大部分是活动的一部分。

  2. An Activity may contain 0 or multiple number of fragments based on the screen size. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.

    根据屏幕大小,活动可能包含0或多个片段。片段可以在多个活动中重用,因此它在活动中充当可重用组件。

  3. A fragment can't exist independently. It should be always part of an activity whereas activity can exist without any fragment in it.

    一个片段不能独立存在。它应该始终是活动的一部分,而活动可以不包含任何片段。

#3


15  

As per the android developer documentation, difference between fragment & activity in their life cycle.

根据android developer文档,片段和活动在生命周期中的差异。

Doc link http://developer.android.com/guide/components/fragments.html#Lifecycle

文档链接http://developer.android.com/guide/components/fragments.html生命周期

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.

活动和片段之间生命周期中最重要的区别是一个活动如何存储在其各自的后堆栈中。默认情况下,活动被放置在系统停止时管理的活动的后堆栈中(以便用户可以使用back按钮导航回活动堆栈,如任务和后堆栈中所讨论的那样)。但是,只有当您在删除片段的事务期间通过调用addToBackStack()显式地请求保存实例时,才会将片段放置到宿主活动管理的后堆栈中。

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.

否则,管理片段生命周期与管理活动生命周期非常相似。因此,管理活动生命周期的相同实践也适用于片段。不过,您还需要了解的是,活动的生命如何影响片段的生命。

& for multi pane layouts you have to use fragment that you can't achieve with activity.

对于多窗格布局,您必须使用活动无法实现的片段。

#4


9  

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.

活动是应用程序的UI,用户可以通过它进行交互,而片段是活动的一部分,它是活动内部的子活动,它有自己的生命周期,与活动生命周期并行运行。

Activity LifeCycle                           Fragment LifeCycle
onCreate()                                    onAttach()
    |                                              |
onStart()______onRestart()                     onCreate()
    |             |                                |
onResume()        |                            onCreateView()
    |             |                                |
onPause()         |                            onActivityCreated()
    |             |                                |
onStop()__________|                             onStart()
    |                                              |
onDestroy()                                     onResume()
                                                   |
                                                onPause()
                                                   |
                                                onStop()
                                                   |
                                                onDestroyView()
                                                   |
                                                onDestroy()
                                                   |
                                                onDetach()

#5


7  

Main Differences between Activity and Fragment

活动和片段之间的主要区别

  1. Activity is an application component which give user interface where user can interect. Fragment is a part of an activity,which contribute its own UI to that activity.
  2. Activity是一个应用程序组件,它提供用户可以进行交互的用户界面。片段是活动的一部分,它为该活动提供自己的UI。
  3. For Tablet or if mobile is in landscape then Using fragment we can show two list like onle list for show the state name and other list will show the state description in single activity but using Activity we can't do same thing.
  4. 对于平板电脑或者移动设备,如果使用片段,我们可以显示两个列表,比如onle列表显示状态名,其他列表显示单个活动中的状态描述,但是使用activity,我们不能做同样的事情。
  5. Activity is not dependent on fragment.but Fragment is dependent on Activity,it can't exist independentaly.
  6. 活动不依赖于片段。但碎片依赖于活动,它不能独立存在。
  7. without using fragment in Activity we can't create multi-pane UI.but using multiple fragments in single activity we can create multi-pane UI.
  8. 如果不在活动中使用片段,我们就不能创建多窗格UI。但是在单个活动中使用多个片段可以创建多窗格UI。
  9. If we create project using only Activity then its difficult to manage but if we use fragments then project structure will be good and we can handle easily.
  10. 如果我们只使用活动来创建项目,那么它就很难管理,但是如果我们使用片段,那么项目结构就会很好,我们可以很容易地处理。
  11. An activity may contain 0 or multiple number of fragments. A fragment can be reused in multiple activities, so its act like a reusable component in activities.
  12. 一个活动可能包含0或多个片段。一个片段可以在多个活动中重用,因此它就像活动中的可重用组件。
  13. Activity has own life cycle but fragment has there own life cycle.
  14. 活动有自己的生命周期,而片段有自己的生命周期。
  15. For Activity we must need to mention in Manifest but for fragment its not required.
  16. 对于活动,我们必须在Manifest中提到,但对于片段,它不是必需的。

#6


3  

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

活动1。活动是Android平台上应用程序的基本构建模块之一。它们是用户与应用程序交互的入口点,也是用户在应用程序或应用程序2之间导航的核心。生命周期方法由OS托管。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所示。一个片段的生命周期

#7


1  

Android app’s entry point is Activity. Activity represents a screen and it provides user interface and behavior to handle input events. A request in the form of an intent is sent to start any activity in the app, meaning android apps have multiple entry points. Android system reads configuration about activities and instantiates them when a request is made for activities. System calls callback methods which enable you to implement certain behavior at different points in the lifecycle of the activity.

Android应用的切入点是活动。Activity代表一个屏幕,它提供用户界面和行为来处理输入事件。以意图的形式发送请求来启动应用程序中的任何活动,这意味着android应用程序有多个入口点。Android系统读取有关活动的配置并在对活动发出请求时实例化它们。系统调用回调方法,使您能够在活动生命周期的不同点实现某些行为。

Fragments on the other hand can’t be started by sending request to the system. They are part of the Activity. Fragments allows you to break down the activity into smaller components so that you can reuse them and using fragments, you can implement a task with flow of multiple sub tasks with just one activity. Fragment has lifecycle using which you can implement behavior when Fragment is in different states of its lifecycle.

另一方面,不能通过向系统发送请求来启动片段。他们是活动的一部分。碎片允许您将活动分解为更小的组件,以便您可以重用它们并使用片段,您可以实现一个任务,其中包含多个子任务流,其中只有一个活动。片段具有生命周期,当片段处于其生命周期的不同状态时,您可以使用它实现行为。

#1


33  

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

活动是提供屏幕的应用程序组件,用户可以使用该组件进行交互以完成某些操作。更多信息: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

而片段表示活动中的行为或用户界面的一部分。http://developer.android.com/guide/components/fragments.html

#2


33  

  1. Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity, where as the complete screen with which user interacts is called as activity. An activity can contain multiple fragments. Fragment are mostly a part of an activity.

    片段是活动的一部分,它为该活动提供自己的UI。片段可以看作子活动,其中作为用户交互的完整屏幕被称为活动。一个活动可以包含多个片段。碎片大部分是活动的一部分。

  2. An Activity may contain 0 or multiple number of fragments based on the screen size. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.

    根据屏幕大小,活动可能包含0或多个片段。片段可以在多个活动中重用,因此它在活动中充当可重用组件。

  3. A fragment can't exist independently. It should be always part of an activity whereas activity can exist without any fragment in it.

    一个片段不能独立存在。它应该始终是活动的一部分,而活动可以不包含任何片段。

#3


15  

As per the android developer documentation, difference between fragment & activity in their life cycle.

根据android developer文档,片段和活动在生命周期中的差异。

Doc link http://developer.android.com/guide/components/fragments.html#Lifecycle

文档链接http://developer.android.com/guide/components/fragments.html生命周期

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.

活动和片段之间生命周期中最重要的区别是一个活动如何存储在其各自的后堆栈中。默认情况下,活动被放置在系统停止时管理的活动的后堆栈中(以便用户可以使用back按钮导航回活动堆栈,如任务和后堆栈中所讨论的那样)。但是,只有当您在删除片段的事务期间通过调用addToBackStack()显式地请求保存实例时,才会将片段放置到宿主活动管理的后堆栈中。

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.

否则,管理片段生命周期与管理活动生命周期非常相似。因此,管理活动生命周期的相同实践也适用于片段。不过,您还需要了解的是,活动的生命如何影响片段的生命。

& for multi pane layouts you have to use fragment that you can't achieve with activity.

对于多窗格布局,您必须使用活动无法实现的片段。

#4


9  

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.

活动是应用程序的UI,用户可以通过它进行交互,而片段是活动的一部分,它是活动内部的子活动,它有自己的生命周期,与活动生命周期并行运行。

Activity LifeCycle                           Fragment LifeCycle
onCreate()                                    onAttach()
    |                                              |
onStart()______onRestart()                     onCreate()
    |             |                                |
onResume()        |                            onCreateView()
    |             |                                |
onPause()         |                            onActivityCreated()
    |             |                                |
onStop()__________|                             onStart()
    |                                              |
onDestroy()                                     onResume()
                                                   |
                                                onPause()
                                                   |
                                                onStop()
                                                   |
                                                onDestroyView()
                                                   |
                                                onDestroy()
                                                   |
                                                onDetach()

#5


7  

Main Differences between Activity and Fragment

活动和片段之间的主要区别

  1. Activity is an application component which give user interface where user can interect. Fragment is a part of an activity,which contribute its own UI to that activity.
  2. Activity是一个应用程序组件,它提供用户可以进行交互的用户界面。片段是活动的一部分,它为该活动提供自己的UI。
  3. For Tablet or if mobile is in landscape then Using fragment we can show two list like onle list for show the state name and other list will show the state description in single activity but using Activity we can't do same thing.
  4. 对于平板电脑或者移动设备,如果使用片段,我们可以显示两个列表,比如onle列表显示状态名,其他列表显示单个活动中的状态描述,但是使用activity,我们不能做同样的事情。
  5. Activity is not dependent on fragment.but Fragment is dependent on Activity,it can't exist independentaly.
  6. 活动不依赖于片段。但碎片依赖于活动,它不能独立存在。
  7. without using fragment in Activity we can't create multi-pane UI.but using multiple fragments in single activity we can create multi-pane UI.
  8. 如果不在活动中使用片段,我们就不能创建多窗格UI。但是在单个活动中使用多个片段可以创建多窗格UI。
  9. If we create project using only Activity then its difficult to manage but if we use fragments then project structure will be good and we can handle easily.
  10. 如果我们只使用活动来创建项目,那么它就很难管理,但是如果我们使用片段,那么项目结构就会很好,我们可以很容易地处理。
  11. An activity may contain 0 or multiple number of fragments. A fragment can be reused in multiple activities, so its act like a reusable component in activities.
  12. 一个活动可能包含0或多个片段。一个片段可以在多个活动中重用,因此它就像活动中的可重用组件。
  13. Activity has own life cycle but fragment has there own life cycle.
  14. 活动有自己的生命周期,而片段有自己的生命周期。
  15. For Activity we must need to mention in Manifest but for fragment its not required.
  16. 对于活动,我们必须在Manifest中提到,但对于片段,它不是必需的。

#6


3  

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

活动1。活动是Android平台上应用程序的基本构建模块之一。它们是用户与应用程序交互的入口点,也是用户在应用程序或应用程序2之间导航的核心。生命周期方法由OS托管。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所示。一个片段的生命周期

#7


1  

Android app’s entry point is Activity. Activity represents a screen and it provides user interface and behavior to handle input events. A request in the form of an intent is sent to start any activity in the app, meaning android apps have multiple entry points. Android system reads configuration about activities and instantiates them when a request is made for activities. System calls callback methods which enable you to implement certain behavior at different points in the lifecycle of the activity.

Android应用的切入点是活动。Activity代表一个屏幕,它提供用户界面和行为来处理输入事件。以意图的形式发送请求来启动应用程序中的任何活动,这意味着android应用程序有多个入口点。Android系统读取有关活动的配置并在对活动发出请求时实例化它们。系统调用回调方法,使您能够在活动生命周期的不同点实现某些行为。

Fragments on the other hand can’t be started by sending request to the system. They are part of the Activity. Fragments allows you to break down the activity into smaller components so that you can reuse them and using fragments, you can implement a task with flow of multiple sub tasks with just one activity. Fragment has lifecycle using which you can implement behavior when Fragment is in different states of its lifecycle.

另一方面,不能通过向系统发送请求来启动片段。他们是活动的一部分。碎片允许您将活动分解为更小的组件,以便您可以重用它们并使用片段,您可以实现一个任务,其中包含多个子任务流,其中只有一个活动。片段具有生命周期,当片段处于其生命周期的不同状态时,您可以使用它实现行为。