Reputation: 2060
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
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.
startActivityForResult(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.Interface
driven implementation and its very easy as compare to communication
between Activites
. Please checkout following link for details.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?
"Fragment & Activity"
and "Fragment & Fragment via Activity"
For Activity B
to Activity A
communication Activity B
will
destroy and pass its result to Activity A
.
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
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