DialogFragment Android Pass Arguments Or Parameters? – Easy & Quick 1 Min Tutorial!

You are currently viewing DialogFragment Android Pass Arguments Or Parameters? – Easy & Quick 1 Min Tutorial!
Photo by Denny Müller on Unsplash
Share This Post, Help Others, And Earn My Heartfelt Appreciation! :)
4.9
(874)

In this short post I will show [ DialogFragment Android Pass arguments ] you how to dynamically provide arguments to DialogFragment from another Fragment or Activity.

NewInstance -> DialogFragment Android pass arguments

Let’s create static public newInstance method in your code.

    static public TestDialogFragment newInstance(String testStringArg) {
        TestDialogFragment f = new TestDialogFragment();

        // Add provided argument to Bundles
        Bundle args = new Bundle();
        args.putString("testStringArgKey", testStringArg);
        f.setArguments(args);
        return f;
    }

And next in the onCreateDialog method just get this argument value like in the code below.

    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        ...
        final String testStringArgValue = getArguments().getString("testStringArgKey");
        ...
    }

Finally

Now you can pass arguments from your entry Fragment or Activity.

DialogFragment newFragment = TestDialogFragment.newInstance("BigDataETL is awesome!");

If you enjoyed this post please add the comment below and share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 874

No votes so far! Be the first to rate this post.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments