Search code examples
androidandroid-activityandroid-fragmentsfragmentandroid-fragmentactivity

Activity with fragment vs Activity


I see that with the latest version of Android SDK they have started offering An Activity with Fragment as well a a blank Activity.

What I would Like to know is when should I use the one over the other?

Should I start using an activity with fragment for all my all my activities? Or do I only use the fragment activity when I think that there might be a change of combining two activities together for a bigger screen device?

I don't really know?

What is the general rule of thumb / best practice?

And how should one lay it out?

Should you have your view in the fragment and the application logic in the Activity? Or does one actually put the application login in the fragment?

The way I tend to want to do it is almost Like a MVC pattern in Web dev. Application is the controller, Fragment is the view


Solution

  • It really depends on your use case. If it's just a simple app, you can get away with just Activities. The benefit of doing it in Fragments is that you can re-use that same Fragment in multiple places as well as show multiple Fragments in one Activity.

    A common example here is the following. Say you have a screen with a list of items. Clicking on one of those items will take you to a new screen with details about that item. On a phone, you will want to display the list on one screen, the details on another. But on a tablet you will want to show both on the screen at a time. So in this case, using fragments is ideal as you will implement them once and be able to use the same fragments in both cases.