我正在尝试实现从AppCompatActivity(或常规Activity)加载的PreferenceFragment,我似乎已经进入了偏好片段的onCreate方法,但是屏幕显示为空白。下面是一张图片:
我有一种感觉,片段正在布局后面加载,但我不知道如何检查这个,因为即使我在一个带有单个FrameLayout的空活动布局上运行它,我也会得到类似的行为。我尝试使用onOptionSelected()在我的主界内显示设置页面。
我已经坚持了一个星期而且没有找到任何答案,我会非常感谢任何帮助!
以下是我的活动代码:
public class genericActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Main.appTAG, "onCreate: settings activity");
setContentView(R.layout.activity_generic);
// IF intent message == settings
final Fragment settingsFragment = new SettingsFragment();
getFragmentManager()
.beginTransaction()
.replace(R.id.fragmentParent, settingsFragment)
.addToBackStack(null)
.commit();
getFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
getFragmentManager()
.beginTransaction()
.remove(settingsFragment)
.commit();
}
});
}
}
活动的布局,即activity_generic.xml如下
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.thesis.deefunkt.thesispractise.genericActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/fragmentParent"
android:fitsSystemWindows="true">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list">
</ListView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
我真的不需要兼容性功能,因为所有这些屏幕都是设置,但保持一致的外观。
我的SettingsFragment代码如下:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
Log.d(Main.appTAG, "onCreate: SettingsFragment ");
}
}
当它运行时,我有一堆日志条目,我不知道如何解释:
D/com.deefunkt.UsbComms: onCreate: settings activity
D/PhoneWindow: *FMB* installDecor mIsFloating : false
D/PhoneWindow: *FMB* installDecor flags : -2139029248
D/AbsListView: Get MotionRecognitionManager
D/Activity: performCreate Call Injection manager
W/Resources: Converting to string: TypedValue{t=0x10/d=0x2580 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x8 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x1 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
D/com.deefunkt.UsbComms: onCreate: SettingsFragment
I/InjectionManager: dispatchPreferences > Target : com.thesis.deefunkt.thesispractise.SettingsFragment
D/AbsListView: Get MotionRecognitionManager
I/InjectionManager: dispatchOnViewCreated > Target : com.thesis.deefunkt.thesispractise.SettingsFragment isFragment :true
I/InjectionManager: dispatchOnViewCreated > Target : com.thesis.deefunkt.thesispractise.genericActivity isFragment :false
D/SecWifiDisplayUtil: Metadata value : SecSettings2
D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1
D/OpenGLRenderer: endAllStagingAnimators on 0x7f65c06600 (RippleDrawable) with handle 0x7f74cff540
如您所见,片段内的onCreate方法被调用,但我不知道如何处理这些日志数据。
提前致谢!
答案 0 :(得分:0)
好的,所以在花了这么多时间之后,我终于设法修复了这个bug,但我不知道为什么。我最好的猜测是
getFragmentManager()
.beginTransaction()
.replace(R.id.fragmentParent, settingsFragment)
/*Adding to the backStack here and then setting a backStack Listener
*on the next step made it add the fragment then immediately take it
*off, giving my empty activity layout.*/
.addToBackStack(null)
.commit();
getFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
getFragmentManager()
.beginTransaction()
.remove(settingsFragment)
.commit();
}
});
我删除了对addToBackStack()的调用并注释掉了backStack监听器,因为我意识到在活动加载之后我不需要删除该片段,只关心重用多个片段的活动。