【问题标题】:Dialog Fragment is not appearing after device is rotated旋转设备后不出现对话框片段
【发布时间】:2020-01-27 11:52:25
【问题描述】:

当我旋转设备时,我有一个对话框片段,对话框片段没有出现。
请看图片以更好地理解

[![这是对话框的外观][1]][1]

[![这就是我想要的样子][2]][2]

启动对话框

ft = activity.supportFragmentManager.beginTransaction()
                //show the fragment
                userDialogFragment.show(ft, "user")

【问题讨论】:

    标签: android kotlin fragment dialogfragment


    【解决方案1】:

    我想你错过了调用setRetainInstance(true)。它控制是否在 Activity 重新创建(例如从配置更改)中保留片段实例。如果设置,在重新创建活动时片段生命周期将略有不同

    其他参考:https://stackoverflow.com/a/15444485/1992013

    【讨论】:

    • 谢谢,但它没有解决我的问题
    • 这里提到了兼容性库中的错误和可能的修复。你可能想检查一下(还有其他方面)。 exceptionshub.com/…
    【解决方案2】:

    当我遇到这个问题时,那是因为我有一张类似的支票

    if(savedInstance == null{
          do...
    }
    

    在我的片段和主要活动中。这阻止了我的片段被重新连接。所以我删除了我的片段活动中的检查。我还通过下面的代码手动控制旋转(允许我支持两个横向片段和一个纵向片段

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            if (Orientation.equals("L")){return;}
            setContentView(R.layout.activity_land);
            Orientation = "L";
        }
    
        else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            if (Orientation.equals("P")){return;}
            setContentView(R.layout.activity_main);
            Orientation = "P";
        }
    }
    

    连同“android:configChanges="orientation"。 虽然这可能无法解决您的问题,但更多代码会有所帮助。

    【讨论】:

    • 我不明白在哪里放置 onConfigurationChanged 代码
    • 您可以将 onConfigurationChanged 代码放在不在函数中的主活动类中的任何位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 2016-10-02
    • 2019-05-31
    • 2014-10-24
    相关资源
    最近更新 更多