1

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.

ElefantPhace
  • 3,806
  • 3
  • 20
  • 36

3 Answers3

2

This is how to do it: Have a Single Activity - N-Number of fragments !


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 !

Community
  • 1
  • 1
Devrath
  • 42,072
  • 54
  • 195
  • 297
1

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.

DeeV
  • 35,865
  • 9
  • 108
  • 95
0

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.

Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
  • Yes, I understand that much of it. But take my example above, using a PreferenceFragment. I want it to completely replace the container, not be beside it. I can do this much, what I'm asking if my approach is acceptable, or should I launch a new Activity to handle this? – ElefantPhace Oct 18 '14 at 15:10
  • You may be using navigation drawer to replace fragments on item click of navigation drawer. yaa you can pop up a new activity for settings,its noting wrong in that. It only depends on usability. – Suhail Mehta Oct 18 '14 at 15:15
  • nope, not using a navdrawer, and I know that i **can** do that. I'm asking what's the best practice for doing this – ElefantPhace Oct 18 '14 at 15:30