KaliMa
KaliMa

Reputation: 2060

DialogFragment versus an Activity themed as a Dialog?

If I want to display a Dialog with some custom XML, I can use a DialogFragment.

However I can also use an Activity instead and give it a Dialog theme, which is convenient because Activities can talk to each other whereas fragments cannot, which makes communication a little easier.

Is there any good reason why I shouldn't be doing it this way? What are the pros and cons?

Upvotes: 4

Views: 2646

Answers (2)

Sagar Trehan
Sagar Trehan

Reputation: 2446

If I want to display a Dialog with some custom XML, I can use a DialogFragment.

Yes we can define the layout in onCreateView() template method callback of DialogFragment.onCreateView() for defining custom layout.

However I can also use an Activity instead and give it a Dialog theme, which is convenient because Activities can talk to each other whereas fragments cannot, which makes communication a little easier.

  1. Yes you can set the same them for activity to make it appear like a dialog.
  2. For two activities(A & B) to communicate with each other you have to usestartActivityForResult(Intent intent, int reqCode)method which start Activity B from Activity A. When Activity B finishes you have to pass result to Activity A via setResult(int reqCode, Intent intent) method.
  3. For activity and fragment communication we have to use Interface driven implementation and its very easy as compare to communication between Activites. Please checkout following link for details.
  4. Two Fragment on same activities can easily communicate with each other via Interface. Please check above link for details. For Tablet UI we have to use fragment to make implementation manageable and modular.

Is there any good reason why I shouldn't be doing it this way? What are the pros and cons?

  1. If you stick with Activity driven development then porting same codebase to tablet is very difficult.
  2. If you use fragment for small-small functionality your codebase will be modular.
  3. Two way communication is possible between "Fragment & Activity" and "Fragment & Fragment via Activity"
  4. For Activity B to Activity A communication Activity B will destroy and pass its result to Activity A.

  5. Activity.startActivityForResult() has its own advantage. Like if you want to capture a image via camera then we use Implicit Intent to start camera Activity and once it captures the image it passes URI of image back in setResult(int reqCode, Intent intent) and hence calling activity onActivityResult() gets invoked so that it can check URI details of image.

Upvotes: 3

Arūnas Bedžinskas
Arūnas Bedžinskas

Reputation: 718

DialogFragment is considered as the Android way to display complex dialog. If the only problem you have is communicating with activity or fragment from dialog - there are some simple solutions. One of them - to send a response to an activity from fragment you can use interface. Also, Displaying DialogFragment is faster than launching a new activity. Another advantage for DialogFragment is that passing data from fragment to activity is easier than activity to activity (this requires to put objects as intent extras).

Upvotes: 1

Related Questions