Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling DialogFragment from Fragment (not FragmentActivity)?

Tags:

I do have a FragmentActivity which contains a Fragment list (with methods to navigate between them). In one of those fragments I need to call a DialogFragment to display a "zoom" on a Picture contained in that fragment.

But it seems that you can't call a DialogFragment directly from a Fragment.

Is there any way to get some kind of "callback" to the FragmentActivity to make this display the DialogFragment over the fragment.

Or simply a "glitch" to call it directly from the Fragment.

If that is the case what are my options?

like image 634
Elie Page Avatar asked Sep 17 '14 09:09

Elie Page


People also ask

How do you get FragmentActivity in a fragment?

use getActivity() method. Which will return the enclosing activity for the fragment.

What is the difference between fragment and FragmentActivity?

If you want to use Fragments in an app targeting a platform version prior to HoneyComb, you need to add the Support Package to your project and use the FragmentActivity to hold your Fragments . The FragmentActivity class has an API for dealing with Fragments , whereas the Activity class, prior to HoneyComb, doesn't.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.


2 Answers

When you create a new Dialog, you can simply call it using this (very) simple method from a Fragment.

DialogFragment dialog = DialogFragment.instantiate(getActivity(), "Hello world"); dialog.show(getFragmentManager(), "dialog"); 

If you want to use your own dialog, please use that kind of code.

public class MyDialogFragment extends DialogFragment {     //private View pic;      public MyDialogFragment()     {     }      @Override     public Dialog onCreateDialog(Bundle savedInstanceState)     {         View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_my_dialog, new LinearLayout(getActivity()), false);          // Retrieve layout elements         TextView title = (TextView) view.findViewById(R.id.text_title);          // Set values         title.setText("Not perfect yet");          // Build dialog         Dialog builder = new Dialog(getActivity());         builder.requestWindowFeature(Window.FEATURE_NO_TITLE);         builder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));         builder.setContentView(view);         return builder;     } } 
like image 181
Manitoba Avatar answered Oct 01 '22 21:10

Manitoba


it will help if you need to show a fragment dialog inside a fragment

DialogFragment

public class DialogBoxFragment extends DialogFragment {     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){         View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);         getDialog().setTitle("simple dialog");         return rootView;     } } 

Now showing the fragment dialog into a fragment

DialogFragment dialogFragment = new DialogFragment ();                             dialogFragment.show(getActivity().getFragmentManager(),"simple dialog"); 
like image 31
PradeepNama Avatar answered Oct 01 '22 20:10

PradeepNama