如何在不显示父级活动的情况下将一个全屏对话框片段替换为另一个? [英] how to replace one full-screen dialog fragment with another without showing parent activity?

查看:42
本文介绍了如何在不显示父级活动的情况下将一个全屏对话框片段替换为另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用单个自定义DialogFragment类的Activity.它的外观是数据驱动的,因此在调用之间看起来可能完全不同.

I have an Activity that uses a single custom DialogFragment class. Its appearance is data driven, so it can look fairly different from invocation to invocation.

这是全屏",即

setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);

响应网络调用的结果,我关闭了当前正在显示的实例(如果有),并关闭了show()一个新的实例:

In response to the result of a network call I dismiss() the currently showing instance (if there is one) and show() a new one:

final CustomDialogFragment dialog = (CustomDialogFragment) getSupportFragmentManager().findFragmentByTag(DIALOG_TAG_CUSTOM);
if (dialog != null)
    dialog.dismiss();
final CustomDialogFragment newdialog = new CustomDialogFragment();
// configure some stuff on the new fragment that influences its appearance
newdialog.show();

这是我的问题:当这段代码运行时,在现有Fragment消失到新的Fragment可见之间,我可以简短地看到底层的Activity.我想以某种方式避免这种情况.

Here is my issue: when this block of code runs, between the point at which the existing Fragment disappears and the new one becomes visible I can briefly see the underlying Activity. I'd like to avoid this somehow.

我的第一个想法是消除新片段的onResume()方法中的现有片段.就是说,尽可能长地延迟"dismiss()"调用,以希望新的片段在取消前一个片段之前就已经可见(遮盖了前一个片段).但这没有效果.

My first thought was to dismiss the existing fragment inside the onResume() method of the new fragment. That is, to delay the "dismiss()" call as long as possible in hopes that the new fragment would already be visible (obscuring the previous one) before the previous one was dismissed. But this had no effect.

我正在考虑的另一个选项是使片段可重新配置",以便我可以推入"新数据并触发它重新绘制其所有视图以匹配新数据.在此解决方案中,我将简单地重新配置现有片段(如果有的话),而不是将其解散并显示一个新片段.

Another option I'm considering is to make the fragment "reconfigurable" so that I can "push in" new data and trigger it to redraw all its views to match the new data. In this solution, I would simply reconfigure the existing fragment (if there is one) instead of dismissing it and showing a new one.

我的问题:在关闭一个全屏DialogFragment并显示另一个全屏DialogFragment时,是否有一种更容易和/或更直接的方法来解决此临时偷看"底层的Activity?

My question: is there an easier and/or more straightforward way to get around this temporary "peek" at the underlying Activity when dismissing one full-screen DialogFragment and showing another?

推荐答案

根据您的活动和全屏片段的外观,会想到一些选择.

Depending on what your activity and full-screen fragments look like, a few options come to mind.

  1. 遵循您自己的第一个想法:尝试将第一个数据驱动的片段发布到根视图的处理程序中,以排定第二个中的第一个数据驱动片段.那就是:

  1. Following your own first thought: try scheduling the dismissal of the first data-driven fragment in the second by posting it to the root view's handler. That is:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {
    View root = ... // inflate view
    root.post(new Runnable() {
         @Override public void run() {
             // create & commit fragment transaction 
             // to remove first fragment here
             }
         };
    return root;
}

这应确保在第二个片段可见后 才执行解雇.如果您使用onCreateDialog(Bundle savedInstanceState)来构建自定义对话框,请确保将其发布到在该视图中代替的视图.

This should ensure that the dismissal doesn't happen until after the second fragment has become visible. If you build a custom dialog using onCreateDialog(Bundle savedInstanceState), make sure you post to the view you inflate there in stead.

实例化两个全屏片段,并使用show()hide()事务在它们之间进行切换(与实际的replace()remove()-和-add()事务相反).这些操作在两个片段之间切换的速度应该快得多,因为在执行事务时,这两个片段已经或多或少已经完全初始化了(换句话说:切换"时的延迟更少).这将对性能造成很小的影响,但可能微不足道.对于以数据为驱动的片段,这是很不理想的,特别是如果您使用参数包来提供数据,但是通过创建一些setter来克服它并不是很难.

Instantiate both full-screen fragments and use show() and hide() transactions to switch between them (as opposed to actual replace() or remove()-and-add() transactions). These operations should be a lot faster to toggle between two fragments, as both will have already been more or less fully initialised upon executing the transaction (in other words: less delay when 'switching'). This will yield a small performance hit, but potentially negligible. It's noy ideal with data-driven fragments, especially if you supply the data using an arguments bundle, but not too hard to overcome by creating some setters.

如果两个全屏片段具有相同的背景(即简单的颜色),则可以为活动的背景赋予相同的颜色.当片段被交换出来时,视觉效果将是一瞬间的空白屏幕,但由于内容发生了变化(因为背景将保持不变),因此侵入性较小.

现在,如果该活动不仅是一个容器,而是显示其自己的内容,则仍然可以通过引入第三个(全屏,不透明)片段作为额外的层来实现上述目的,以遮盖基础活动,同时在全屏片段.只需在执行此操作时将遮盖层保留在适当的位置,并在返回到活动的内容时将其关闭即可.

如果活动和片段具有非常丰富且装饰不同的背景,那么最后一个选择可能并不理想.

If the two full-screen fragments have an identical background (i.e. a simple colour), then you could give the activity's background the same colour. When the fragments then get swapped out, the visual result will be a momentarily blank screen, but less intrusive, as only the content changes (because the background will stay the same).

Now, if the activity is not merely a container, but displays its own content, you can still achieve the same as above by introducing a third (full-screen, opaque) fragment as an extra layer to obscure the underlying activity while switching between the full-screen fragments. Just leave the obscuring layer in place when you do so, and dismiss it when returning to the activity's content.

If the activity and fragments have a very richly and differently decorated background, then this last option may not be ideal.

这篇关于如何在不显示父级活动的情况下将一个全屏对话框片段替换为另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆