Passing Argument to Dialogfragment

passing argument to DialogFragment

Using newInstance

public static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();

// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);

return f;
}

And get the Args like this

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
...
}

See the full example here

http://developer.android.com/reference/android/app/DialogFragment.html

How to pass an argument to dialogFragment

You need to use arguments to pass your values:

class DatePickerFragmentDialog : DialogFragment() {

companion object {
fun newInstance(isStarted: Boolean): DatePickerFragmentDialog {
val f = DatePickerFragmentDialog()
// Supply isStarted input as an argument.
val args = Bundle()
args.putBoolean("isStarted", isStarted)
f.arguments = args
return f
}
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val isStarted = arguments?.getBoolean("isStarted")
// Return some dialog
return super.onCreateDialog(savedInstanceState)
}
}

Show it:

val fragment = DatePickerFragmentDialog.newInstance(true)
fragment.show(supportFragmentManager, "myFragment")

How to pass arguments to dialog in android?

If its a fragment, then there should be always a default constructor available.
Passing arguments separate will ensure that the arguments are preserved across state changes of the fragment

So there is a method setArgument(Bundle) in which you can pass your parameters.
So here your call should be rewritten as

class MyDialog: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity.let {
val arg = arguments
// Use the parameters by accessing the key from variable "arg"
val myBuilder = AlertDialog.Builder(it)
myBuilder
.setTitle(theTitle)
.setMessage(theMessage)
.setPositiveButton("OK") { _, _ -> }
myBuilder.create()
}
}
}

You call you Dialog like this:

val d = MyDialog()
val b = Bundle()
b.putInt("KEY1",1)
d.arguments = b
d.show(FragmentManager,Tag)

For any fragment always remember to use arguments to pass data

Pass data from activity to dialogFragment in android

Since product_data_two.getColor() returns an ArrayList<ArrayList<String>> , you can add it to bundle like this:

bundle.putSerializable("SOME_KEY",product_data_two.getColor());

And in your Fragment, get it like this:

getArgument = (ArrayList<ArrayList<String>>) bundle.getSerializable("SOME_KEY");

read more: Bundle

How to pass data from a fragment to a dialogFragment

What you need is to setArguments on the fragment as follows:

Bundle args = new Bundle();
args.putString("key", "value");
DialogFragment newFragment = new YourDialogFragment();
newFragment.setArguments(args);
newFragment.show(getSupportFragmentManager(), "TAG");

All you have to do now, is catch those arguments in your fragment and use them...

AND IN THE DIALOG FRAGMENT YOU READ IT LIKE THIS...

Bundle mArgs = getArguments();
String myValue = mArgs.getString("keyUsed to send it...");

Passing an argument from the second to the first dialog fragment

I got the answer:

I have added the following code in the second dialog fragment:

void DoneBtn_Click (object sender, EventArgs e)
{
String str = comment.Text;
// the following lines are added
Bundle args = new Bundle ();
args.PutString ("comment", str);
var transaction = FragmentManager.BeginTransaction();
var dialogFragment = new Report();
dialogFragment.Arguments= args;
dialogFragment.Show(transaction, "dialog_fragment");
this.Dismiss ();
}

I have added the following code in the first dialog fragment:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// the following lines are added
Bundle args = Arguments;
if (args != null) {
String returnString = args.GetString ("comment");
}
}

void TextView_Click (object sender, EventArgs e)
{
ShowDialog ();
// dismiss the first dialogFragment
this.Dismiss ();
}

passing context as argument of DialogFragment

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.

Sample Image

For more information, read the Fragment documentation



Related Topics



Leave a reply



Submit