user2528167
user2528167

Reputation:

What is the difference between include a layout or include a fragment in Android?

I am looking my Eclipse IDE for Android and I am thinking that most times I can do same actions including a layout or including a fragment because both can be managed throught Java class. I don't understand very well fragments after I have read many posts... I think I can create a responsive layout without them, only including another layout in my main layout. So... Do you think are there any differences between include a layout or include a fragment in Android? Can you explain me if an option is better than the another one?

Upvotes: 13

Views: 5026

Answers (3)

Sanoob Pookodan
Sanoob Pookodan

Reputation: 25

Commenly I used include, Because its easy to use base level of developers also.Because that its including lay out syntax is 'simple' it doesn't have java part.

In fragmemt ,We can consider a fragment as a sub activity. It has its own life cycle as a normal activity, but the life cycle of parent activity would affect the fragment activity

Mainly commene difference is haven't life cycle.But most developera used fragment for intentation , In fact include have all java function support, it excluded lyfecycle function.

IF YOU HAVE REUSE A PAGE THAT CONDAIN INTENT then you can choos include and easy to handle

Upvotes: 0

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

From the Android docs:

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 (sort of like a "sub activity" that you can reuse in different activities).

The important part of this is that Fragments have their own lifecycle. This means you can start, pause, resume, stop, etc. a Fragment. You can't do any of that with a layout. It's also wise to note that Fragments contain their own layouts.

Upvotes: 7

Alan Paul Mathew
Alan Paul Mathew

Reputation: 219

We can consider a fragment as a sub activity. It has its own life cycle as a normal activity, but the life cycle of parent activity would affect the fragment activity.

But a layout is just a design which can be reuse by 'include' it to an activity. It don't have its own life cycle.

Upvotes: 0

Related Questions