【问题标题】:SettingsFragment : PreferenceFragmentCompat appears black after Activity is removed by the Android SystemSettingsFragment : PreferenceFragmentCompat Activity 被Android系统移除后显示为黑色
【发布时间】:2020-04-18 07:42:46
【问题描述】:

已解决:我在包含我的 NavHostFragment 的 xml 中有一个框架布局。删除此 FrameLayout 后,一切都按原样显示。

原问题如下:

我已经使用开发者设置中的“不保留活动”选项测试了我的应用,因此一旦用户离开活动,系统就会销毁活动。启用此选项后,如果我在设置片段打开时离开我的应用程序,重新打开应用程序时屏幕会显示为黑色。当我点击屏幕时,会打开一个设置对话框。离开对话框后,屏幕再次变黑。当我按下后退按钮时,屏幕保持黑色,但如果我随后离开应用程序并重新打开它,我输入设置的片段将正常呈现。

如果我禁用“不保留活动”选项,则不会发生这种情况,但当系统出于其他原因(释放内存)破坏活动时会发生这种情况。

这是一些看起来相关的代码。

我的 SettingsFragment 是这样的:

class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    setPreferencesFromResource(R.xml.settings, rootKey)
}

}

而我的settings.xml是这样的

<androidx.preference.PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
    app:key="rootkey">
<ListPreference
        app:key="buttonpos"
        app:title="@string/setbuttonpos"
        app:summary="@string/setbuttonposdescription"
        app:defaultValue="c"
        app:entries="@array/buttonposkey"
        app:entryValues="@array/buttonpos"/>
<ListPreference
        app:key="levellength"
        app:title="@string/setlevellength"
        app:summary="@string/setlevellengthdescription"
        app:defaultValue="9"
        app:entries="@array/levellengthkey"
        app:entryValues="@array/levellength"/>
// Some other entries here
</androidx.preference.PreferenceScreen>

编辑:设置片段的打开方式如下:

    optionsbutton.setOnClickListener {
        NavHostFragment.findNavController(fragment5)
            .navigate(R.id.action_global_settingsFragment)
    }

在 nav_graph.xml 中:

 <?xml version="1.0" encoding="utf-8"?>
 <navigation xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph"
        app:startDestination="@id/mainFragment">
     <fragment
        android:id="@+id/settingsFragment" android:name="com.my.app.userinterface.SettingsFragment"
        android:label="SettingsFragment" />
     <action
        android:id="@+id/action_global_settingsFragment"
        app:destination="@id/settingsFragment" />
 </fragment>
 </navigation>

这是包含 NavHostFragment / fragment5 的主 Activity 布局 - 移除内部 FrameLayout 后,一切正常:

<?xml version="1.0" encoding="utf-8"?>

<!-- The primary full-screen view. This can be replaced with whatever view
     is needed to present your content, e.g. VideoView, SurfaceView,
     TextureView, etc. -->
<fragment
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:navGraph="@navigation/nav_graph" app:defaultNavHost="true"
        android:id="@+id/fragment5"/>
<TextView android:id="@+id/fullscreen_content"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:keepScreenOn="true" android:translationZ="-10dp"
          android:textColor="#33b5e5"
          android:textStyle="bold"
          android:textSize="50sp"
          android:gravity="center"
          android:text="@string/dummy_content" android:visibility="visible"/>

<!-- This FrameLayout insets its children based on system windows using
     android:fitsSystemWindows. -->
<FrameLayout android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:fitsSystemWindows="true">

    <LinearLayout android:id="@+id/fullscreen_content_controls"
                  style="?metaButtonBarStyle"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="bottom|center_horizontal"
                  android:background="@color/black_overlay" android:translationZ="-10dp"
                  android:orientation="horizontal"
                  tools:ignore="UselessParent" android:visibility="visible">

        <Button android:id="@+id/dummy_button"
                style="?metaButtonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" android:translationZ="-10dp"
                android:text="@string/dummy_button"/>

    </LinearLayout>
</FrameLayout>

【问题讨论】:

  • 请发布打开设置片段的代码。以及所有与它们周围的ifs 一起进行碎片交易的东西。设置屏幕很可能不是问题。
  • 感谢您为我指明正确的方向。如果您发布合适的答案作为答案,我会接受。
  • 嗯,我不确定为什么它有效。如果你能弄清楚,你可以自己回答这个问题。否则你可以在它解决后删除它,它可能会引起不相关的答案。
  • 我也不知道它为什么会起作用,但我也不想删除这个问题,因为谷歌和 stackoverflow 中的搜索都没有显示任何答案。如果其他人遇到这个问题,他们可能会发现这个问题很有用,即使不清楚它究竟为什么起作用。

标签: android android-fragments settings android-settings


【解决方案1】:

我不确定它为什么起作用,但我在包含我的 NavHostFragment 的 xml 中有一个 FrameLayout。删除该 FrameLayout 后,一切都再次按原样显示。

如果您有解释为什么这样做,请随时将其发布为答案,我会接受。

【讨论】:

    猜你喜欢
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 2012-01-31
    • 1970-01-01
    相关资源
    最近更新 更多