我该如何保存片段?(它位于活动中)当我旋转屏幕时,活动被重新启动,片段被重新创建
在您的fragment的onCreate方法中尝试使用setRetainInstance(true);
。
您需要在AndroidManifest.xml中的android:configChanges中添加orientation。例如:
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
... CustomFragment fragment; if (savedInstanceState != null) { fragment = (CustomFragment) getSupportFragmentManager().findFragmentByTag("customtag"); } else { fragment = new CustomFragment(); getSupportFragmentManager().beginTransaction().add(R.id.container, fragment, "customtag").commit(); } ...
} - Viral