关闭对话片段后刷新片段 [英] Refresh the Fragment on dismiss of Dialogue fragment
问题描述
服务器上有一些同步过程可以更新数据库,我想在单击对话框时单击以刷新父活动,以在视图中获取同步值.
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.
我有三个片段-FragmentA.java
,FragmentB.java
和ragmentC.java
在FragmentA.java
中,我具有公共接口OnEntrySelectedListener
,其具有方法getDialog()
和
其他片段类FragmentB.java
实现此接口并具有getDialog()
方法的定义.
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;
}
现在,我想在关闭FragmentC
时刷新FragmentA
.
Now I want to refresh the FragmentA
on dismiss of FragmentC
.
推荐答案
您可以向C添加接口和回调以执行以下操作:
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();
}
然后在主要活动中,您可以实现该接口,并在收到呼叫时将其转发到片段A.
Then in the main activity you can implement the interface and when you get the call you forward it to fragment A.
您还可以触发FragmentA监听的刷新事件.
You could also fire a refresh event that FragmentA would listen to.
这篇关于关闭对话片段后刷新片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!