问题
There is some synch process on server that updates the database and I want to refresh the parent activity on click of dialogue dismiss to get the synched values in a view.
I have three fragments- FragmentA.java
, FragmentB.java
and ragmentC.java
In FragmentA.java
, I have public interface OnEntrySelectedListener
which have method getDialog()
and
other fragment class FragmentB.java
implements this interface and have definition for getDialog()
method.
FragmentA.java
class FragmentA extends Fragment
{
public interface OnEntrySelectedListener
{
getDialog();
}
}
FragmentB.java
class FragmentB extends FragmentActivity implements FragmentA.OnEntrySelectedListener
{
@Override
public void getDialog(Bundle bundle) {
FragmentC cf = new FragmentC();
cf.setArguments(bundle);
cf.show(getSupportFragmentManager(), "dialog");
}
}
FragmentC.java
class FragmentC extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
//some code here
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.shared,container, false);
view.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
getDialog().dismiss(); //Here after dismiss, I want to refresh FragmentA
}
});
return view;
}
Now I want to refresh the FragmentA
on dismiss of FragmentC
.
回答1:
You can add an interface and callback to C to do something like this:
@Override
public void onDismiss(DialogInterface dialog) {
if (getActivity() != null && getActivity() instanceof Dismissed) {
((Dismissed) getActivity()).dialogDismissed();
}
super.onDismiss(dialog);
}
public interface Dismissed {
public void dialogDismissed();
}
Then in the main activity you can implement the interface and when you get the call you forward it to fragment A.
You could also fire a refresh event that FragmentA would listen to.
来源:https://stackoverflow.com/questions/15619065/refresh-the-fragment-on-dismiss-of-dialogue-fragment