简体   繁体   English

Android DialogFragment解雇

[英]Android DialogFragment Dismissal

I've looked at several of the questions pertaining to handling an orientation change in a DialogFragment, and the common theme seems to be that the askers aren't getting any answers, at none that I understand... 我已经看过与处理DialogFragment中的方向更改有关的几个问题,并且共同的主题似乎是问者没有得到任何答案,我什至没有理解...

So, I'd like to take a different approach... Is there a way I can programmatically dismiss the dialog... Looking at my LogCat, I can see that if the dialogfragment is active, and the screen orientation is changed, then my Activity restarts. 因此,我想采用另一种方法...有没有办法以编程方式关闭对话框...查看我的LogCat,我可以看到,如果对话框片段处于活动状态,并且屏幕方向已更改,则我的活动重新开始。

Along the way, the DialogFragment is also restarted, but this happens after onCreate and before onResume (as close as I can tell). 在此过程中,DialogFragment也重新启动,但这发生在onCreate之后和onResume之前(据我所知)。 It's fairly simple for me to detect this in my dialog, and what I'd like to do is abort the dialog view creation in this case. 对我来说,在对话框中检测到它很简单,在这种情况下,我想做的就是中止对话框视图的创建。 What I've tried so far is like this: 到目前为止,我尝试过的是这样的:

    if (normal_conditions_detected)
    {
        View v = inflater.inflate (R.layout.dialog_layout, container, false);
        final Dialog d = getDialog();
        d.setTitle (R.string.sensor_config);

        ... more stuff
        return v
    }
    else
    {
        getDialog().cancel();
        return null;
    }

This does avoid the null pointer nonsense I was getting, but now I get what I can only describe as an empty dialog, even with the cancel() command in there. 这确实避免了我得到的空指针的废话,但是现在我得到了只能描述为空对话框的内容,即使其中有cancel()命令也是如此。 Is there a way I can get my dialogFragment code to refuse to create the view? 有没有办法让我的dialogFragment代码拒绝创建视图?

When you rotate your device, activity executes 旋转设备时,活动将执行

onSaveInstanceState 

method and save the current state which means it saves your fragment state as well, after recreation it executes 方法并保存当前状态,这意味着它也会在重新创建后保存您的片段状态

onCreate(Bundle savedInstanceState)

method again, savedInstanceState holds your old data, in here you can make something like 再次使用方法,savedInstanceState保存您的旧数据,在这里您可以进行如下操作

if (savedInstanceState == null){
   YourDialogFragment f = new YourDialogFragment()
   f.show // etc
}

Only once your fragment will be created, instead of putting a control inside fragment, you can control it by the activity 仅创建片段之后,您无需通过将控件放入片段中,而可以通过活动来对其进行控制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM