我有片段和配置更改(旋转)的问题:我有一个带有随机更新片段文本的按钮的屏幕。
一切正常,直到我旋转设备(配置更改):我可以findFragmentByTag
片段但它的getView()
方法返回null
。我正在使用 setRetainState(true) :所有私有字段都得到了很好的保留。
在下面的代码中,我正在动态创建片段,因为我处于视图寻呼机的上下文中(TestItemListFragment 指的是 ViewPager 页面的片段,而 TestItemDetailFragment 指的是我希望在单击时更新文本的片段按钮)。尽管对于本示例,显示的代码已简化,但请参阅代码中的注释以了解详细信息。
我错过了什么?我究竟做错了什么 ?(当然我是)
编辑:复制源链接:https ://bitbucket.org/bixibu/demo-nestedfragment-android
活动 :
public class TestActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
Bundle args = new Bundle();
TestItemListFragment frag = TestItemListFragment.newInstance();
if (getSupportFragmentManager().findFragmentByTag("tag") == null) {
getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag, "tag").commitAllowingStateLoss();
}
}
}
页面片段:
public class TestItemListFragment extends Fragment {
ItemDetailFragment detailFragment = null;
String mCurTitle = null;
private SecureRandom random = new SecureRandom();
public static TestItemListFragment newInstance() {
TestItemListFragment fragment = new TestItemListFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.item_list_fragment_test, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/**
* Addind random string generation while clicking on button
* And setting this random String to the fragment
*/
Button button = (Button) getView().findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updateDetail(new BigInteger(130, random).toString(32));
}
});
// creating new fragment
detailFragment = ItemDetailFragment.newInstance(new Bundle());
// in my XML I don't have any ID because I have to create as many fragment as I have Pages in my ViewPager
// so I dynamically creates an ID
// in order to have a different fragment for each Page
// if I don't do that only my first Page display a fragment (or multiple ones)
int fragmentContainerUniqId = 519618565;
View fragmentContainer = getView().findViewById(R.id.detailFragmentContainer);
fragmentContainer.setId(fragmentContainerUniqId);
if (getActivity().getSupportFragmentManager().findFragmentById(fragmentContainerUniqId) == null) {
// Add the fragment to the 'detailFragmentContainer' FrameLayout
getActivity().getSupportFragmentManager().beginTransaction()
.add(fragmentContainerUniqId, detailFragment).commitAllowingStateLoss();
}
if (mCurTitle != null) {
updateDetail(mCurTitle);
}
}
// May also be triggered from the Activity
public void updateDetail(String title) {
detailFragment.setTitle(title);
}
}
细节片段:
public class ItemDetailFragment extends Fragment {
public static ItemDetailFragment newInstance(Bundle bundle) {
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_detail_fragment, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setTitle(getArguments().getString("title"));
}
public void setTitle(String title) {
if (getView() != null && title != null) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(title);
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="populate fragment"
/>
<FrameLayout
android:id="@+id/detailFragmentContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
显现 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.burnside.digital.nestedfragments"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.burnside.digital.nestedfragments.TestActivity"
android:label="@string/title_activity_test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ps:我正在使用 support-v4 lib
感谢您的帮助,我会更新我的帖子,我不清楚,所以让我知道。