Sartheris Stormhammer
Sartheris Stormhammer

Reputation: 2564

Android DialogFragment started inside a Fragment

I have a few fragments, and in one of them I have to ask the user to select time, so from the fragment I open up a DialogFragment with TimePicker and Button, so I want when the button is clicked to update the view in the Fragment which opened the dialog, how can I do that?

Upvotes: 0

Views: 42

Answers (2)

user2212461
user2212461

Reputation: 3253

The easiest way is to replace the fragment calling your context, like this:

public void replaceFrag(){
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, new YourNewFragment());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}

Call this method in your button's onClick() event

this assmumes, that your fragment was programmatically commited into a fragment_container xml tag, which you might need to adapt accordingly.

Upvotes: 0

Itzik Samara
Itzik Samara

Reputation: 2288

public interface DialogListener {

       public void onDialogEnded(Bundle bundle);
}

in the Parent Fragment listen to this DialogListener.

in the Dialog fragment call the DialogListener with null or any kind of bundle with data this will call the listener in the parent Fragment with what you need.

in the DialogFragment create a setup Listener example :

private DialogListener mListener;
public void setListener(DialogListener listener) { mListener = listner; }

when you call the Dialog call the Listener before.

Upvotes: 1

Related Questions