问题描述
让我们说我有一个片段,从中我打开 DialogFragment
是这样的:
Let's say I have Fragment A, from which I open a DialogFragment
like this:
FragmentActivity fragmentActivity = (FragmentActivity) view.getContext();
FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
Fragment prev = fragmentActivity.getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment fragmentDialog = MyDialogFragment.newInstance();
fragmentDialog.show(ft, "dialog");
在该对话框中,单击(正面/中性/负)按钮后,我想开片段B,应更换片段A。
From this Dialog, after clicking (positive / neutral / negative) button, I want to open Fragment B, which should replace Fragment A.
在对话框的onClick方法我运行父活动的回调方法:
In the Dialog's onClick method I run a callback method of parent Activity:
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which) {
case DialogInterface.BUTTON_NEUTRAL:
detailsCallbacks.openMoreDetails();
break;
}
}
最后我活动的 openMoreDetails()
方法是这样的:
@Override
public void openMoreDetails() {
Fragment fragmentB = Fragment.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragmentB);
ft.addToBackStack(null);
ft.commit();
}
我得到的是什么奇怪的。片段B闪烁屏幕只为第二分数,然后是(覆盖?)由片段A更换一次。
What I get is strange. Fragment B blinks on the screen just for the fraction of second and then is replaced (covered?) by Fragment A again.
当我点击了按钮,我得到甚至从片段A回来,这些非交易加入到背堆栈。我想显示片段B,然后当pressing向上按钮返回到片段A
When I click up button I get even back from Fragment A so non of these transaction were added to the back stack. I would like to show Fragment B and then when pressing up button go back to Fragment A.
时它以某种方式可能吗?什么是错上我的方法?
Is it somehow possible? And what's wrong on my approach?
推荐答案
刚刚有同样的问题:
片段A显示自定义对话框片段。
Fragment A display a custom dialog fragment.
在单击对话框片段的按钮中的一个,我想删除对话框并显示片段B
At click on one of the buttons of the dialog fragment, I wanted to remove the dialog and show Fragment B.
片段B被显示,并瞬间消失。我的屏幕再次显示片段A。
Fragment B was displayed and instantly disappear. My screen was displaying Fragment A again.
什么的错误在我最初的实现:
What was wrong on my initial implementation:
private void onClickInscription() {
FragmentInscription frag = FragmentInscription.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main, frag);
ft.addToBackStack(null);
ft.commit();
dismiss();
}
以及正确之一:
private void onClickInscription() {
dismiss();
FragmentInscription frag = FragmentInscription.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main, frag);
ft.addToBackStack(null);
ft.commit();
}
所以要尽量先打电话解雇你的对话框然后应用 FragmentTransction
这篇关于从DialogFragment打开片段(替换对话框父)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!