Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use DialogFragment?

Basically Dialogs inactivates the activity at the background. So the DialogFragment does the same with increased complexity. So why should one go for DialogFragment though various subclasses of Dialog are available.

like image 934
Chaitanya K Avatar asked Aug 28 '12 09:08

Chaitanya K


People also ask

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

Is DialogFragment deprecated?

This method is deprecated. androidx.


2 Answers

Fragments are used with in your activity, but to present a fragment as dialog (window) using FragmentTransaction and followup with fragment's life-cycle, you need to use DialogFragment. However, you may do use simple Dialog too, but then it has nothing to do with the fragment's life-cycle.

As per google docs:

A DialogFragment can still optionally be used as a normal fragment, if desired. This is useful if you have a fragment that in some cases should be shown as a dialog and others embedded in a larger UI.

like image 56
waqaslam Avatar answered Nov 05 '22 13:11

waqaslam


FragmentDialog is a fragment that can be:

  1. used as fragment, e.g.:

    FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
    trans.add(R.id.navigation_fragment, mFriendFragment);
    trans.commit();
    
  2. used as dialog, e.g.:

    FragmentManager fm = getFragmentManager();
    UnsubscribeTabletFragment fragment = new UnsubscribeTabletFragment();
    fragment.show(fm, "dialog");
    

So, if you have a fragment, and the fragment sometimes works as fragment, sometimes works as dialog, then you should use this one.

like image 44
Kai Wang Avatar answered Nov 05 '22 13:11

Kai Wang