What are the differences between activity and fragment?

£可爱£侵袭症+ 提交于 2019-12-27 13:38:23

问题


As per my research, there is a significant difference in the concept of backstack and how they exist:

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.

  • 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.

  • Fragment has to live inside the activity

Are there any additional differences?


回答1:


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

Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html




回答2:


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

Doc link 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.

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




回答3:


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 contibute its own UI to that activity.
  2. 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.
  3. Activity is not dependent on fragment.but Fragment is dependent on Activity,it can't exist independentaly.
  4. 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.
  5. 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.
  6. 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.
  7. Activity has own life cycle but fragment has there own life cycle.
  8. For Activity we must need to mention in Manifest but for fragment its not required.



回答4:


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 LifeCycle                           Fragment LifeCycle
onCreate()                                    onAttach()
    |                                              |
onStart()______onRestart()                     onCreate()
    |             |                                |
onResume()        |                            onCreateView()
    |             |                                |
onPause()         |                            onActivityCreated()
    |             |                                |
onStop()__________|                             onStart()
    |                                              |
onDestroy()                                     onResume()
                                                   |
                                                onPause()
                                                   |
                                                onStop()
                                                   |
                                                onDestroyView()
                                                   |
                                                onDestroy()
                                                   |
                                                onDetach()



回答5:


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

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



来源:https://stackoverflow.com/questions/25822656/what-are-the-differences-between-activity-and-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!