Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing context as argument of DialogFragment

it's possible to pass a context variable to a DialogFragment?

i've use this code inside dialog for passing a string:

public static ConfirmDialog newInstance( String f) {     ConfirmDialog d = new ConfirmDialog();      Bundle args = new Bundle();     args.putString("FILE_NAME", f);     d.setArguments(args);      return d; } 

but i don't find any function like putString for passing context. It's possible to do that?

like image 573
giozh Avatar asked Mar 17 '13 18:03

giozh


People also ask

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.

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 class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.


2 Answers

Your DialogFragment has a very handy method for getting a Context instance:

getActivity() 

Fragment#getActivity() will return the instance of the Activity (which is a Context) that the Fragment is attached to. Use it after the Fragment's onAttach() is called. The below chart illustrates the Fragment lifecycle, as you can see, using getActivity() from onCreate() to onDestroy() should be a valid call.

enter image description here

For more information, read the Fragment documentation

like image 169
A--C Avatar answered Sep 18 '22 18:09

A--C


@Override public void onAttach(Activity activity) {     // TODO Auto-generated method stub     super.onAttach(activity);     context=activity; } 

Need to use onAttach method : for dialog Fragment

like image 37
taran mahal Avatar answered Sep 17 '22 18:09

taran mahal