Reputation: 3814
I keep running into issues while having only one Activity
with a container Fragment
that I then replace as needed. Is this the correct way to do this, or should each Fragment
have it's own Activity
?
For example with a PreferenceFragment
is it acceptable to do something like this from my MainActivity
:
fm.beginTransaction()
.replace(R.id.container, new SettingsFragment())
.addToBackStack(null)
.commit();
Or would it be better to just launch a new Activity
(e.g. SettingsActivity
) that handles the Fragment
transactions?
Main issues revolve around rotating device, but that's a separate question.
Upvotes: 1
Views: 124
Reputation: 36035
Activities are generally small programs that serve a single purpose. They can range from very specific and small to rather complex. Fragments are individual parts of an Activity that are intended to work together to do all the work the Activity requires. A Fragment is intended to do one thing and one thing well. If it's to display an article, that's what it does. It doesn't care about a list of articles, or changing user preferences or anything like that. So if you have one Activity that's only intended to display an article, then that's the only fragment you'll have.
If you use one fragment per Activity, then you are basically moving to the old way of doing things (pre-fragment). There's absolutely nothing wrong with this. It's the way things were after all. It will just increase clutter possibly since you will have a Fragment file and an Activity file as well as other things.
So a good example may be a Gallery app. You will initially have one Activity with three loosely-coupled Fragments that it swaps out.
GalleryFragment - Shows a list of pictures and videos available.
PictureFragment - Shows a picture with zoom in and rotate features.
VideoFragment - Shows a video with playback controls.
Like Suhail Mehta said, you could show these Fragments together on a tablet if your design wanted and you'd have very little to change.
Later on, you decide to allow your Gallery app to be shared by other 3rd parties via implicit Intent. To do this, you'd make two more Activities:
PictureActivity - Shows a picture provided. Uses only the PictureFragment.
VideoActivity - Shows only a video. Uses only the VideoFragment.
So with that, it's very easy to add, remove, and update features.
Upvotes: 1
Reputation: 42824
This is how to do it: Have a Single Activity - N-Number
of fragments !
Replacing
,
Attaching
, Detaching
For your Question: would it be better to just launch a new Activity (e.g. SettingsActivity) that handles the Fragment transactions?
Ans:: You can have a single Activity, another activity is not required !
Best Practice :: It is better to have a single activity and multiple fragments
Reason: Fragments give you the advantage of reuse and lot of advanced controls work on fragments !
Upvotes: 2
Reputation: 5542
On each fragment should not have its own activity. Fragments are used to have single navigation approach or to have multi-pane UI .
A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. But it doesn't mean you embedded each fragment in new activity,fragments were not meant for that purpose.
A news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity .
So , its right practise to use fragments within an activity depending upon you UX/UI.
Upvotes: 0