What is the difference between Activity, FragmentActivity and Fragment?

12

What are the differences in performance, performance, etc. in inherit from each of the classes in the controllers of views (XML) on Android?

    
asked by anonymous 12.05.2016 / 15:54

2 answers

6

Both Activity like Fragment are components that provide a graphical interface (UI), to allow the user to interact with the application. > The fundamental difference is that a Fragment needs an Activity to be presented.

Fragment was introduced in Android 3.0 (Level 11 API) and its main purpose is to allow greater flexibility in the creation of UI adaptable to the various dimensions of screens in existing devices. It allows you to centralize the code for a part (fragment) of the UI.
By dividing the layout of an Activity into fragments, you can modify the appearance of the Activity at run time simply. Another advantage is the reuse of the code, since they can be used in more than one Activity.

Some people use Fragment without an associated view to implement the MVP pattern. The fragment makes presenter , taking advantage of the possibility that this has, calling the method setRetainInstance (true) , not to be destroyed during the re-creation of Activity (as the result of device rotation), while maintaining application state.

The FragmentActivity is a "wrapper" around an Activity to allow Fragment to be used on devices running Android prior to version 3.

    
12.05.2016 / 19:04
3

I would respond through a comment because my knowledge on this aspect is limited, but it would get very large.

The difference, if any, is between Activities and Fragments . Performance I believe there is not. As for functionality, Fragments , because they are reusable, allow more complex interfaces than Activities with simpler code.

As far as my little experience with MVP / MVC for Android goes, both Activities and Fragments require attention due to their lifecycles. At the moment I can not see if using Fragments makes communication between components more complicated, but I think minimizing this complication is one of the intentions of using MVP / MVC.

When it comes to other classes ( FragmentActivity , ActionBarActivity , AppCompatActivity ), they exist for compatibility with older versions of Android. This answer in SOen illustrates the differences between them.

PS: If you're talking about MVVM using Data Binding, then there's nothing I know. :)

    
12.05.2016 / 17:33