我在 fragment 和配置更改(旋转)方面遇到问题:我有一个带有随机更新 fragment 文本的按钮的屏幕。
一切正常,直到我旋转设备(配置更改):我可以 findFragmentByTag
fragment ,但它的 getView()
方法返回 null
.
我正在使用 setRetainState(true) :所有私有(private)字段都得到很好的保留。
在下面的代码中,我正在动态创建 fragment ,因为我处于 View 寻呼机的上下文中(TestItemListFragment 指的是 ViewPager Page 的 fragment ,而 TestItemDetailFragment 指的是我希望更新文本的 fragment 当点击按钮时)。尽管对于此示例,显示的代码已简化,但请参阅代码中的注释以了解详细信息。
我错过了什么?我究竟做错了什么 ? (我当然是)
编辑:复制来源链接:https://bitbucket.org/bixibu/demo-nestedfragment-android
Activity :
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();
}
}
}
页面 fragment :
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);
}
}
详细 fragment :
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>
list :
<?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
谢谢你的帮助,我会更新我的帖子,如果有什么不清楚的话,请告诉我。
最佳答案
屏幕旋转后,您的 fragment (在屏幕上可见)不会更新,因为它与 detailFragment 不是同一个 fragment 。
屏幕旋转后,您创建一个新 fragment :
detailFragment = ItemDetailFragment.newInstance(new Bundle());
但它不会被添加到 fragmentContainerUniqId 因为
getActivity().getSupportFragmentManager().findFragmentById(fragmentContainerUniqId)
不为空
通过此更改 TestItemListFragment 中的代码, fragment 将被更新
@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));
}
});
// 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 ((detailFragment = (ItemDetailFragment) getActivity().getSupportFragmentManager().findFragmentById(fragmentContainerUniqId)) == null) {
// Add the fragment to the 'detailFragmentContainer' FrameLayout
// creating new fragment
detailFragment = ItemDetailFragment.newInstance(new Bundle());
getActivity().getSupportFragmentManager().beginTransaction()
.add(fragmentContainerUniqId, detailFragment).commitAllowingStateLoss();
}
if (mCurTitle != null) {
updateDetail(mCurTitle);
}
}
关于android - 旋转后无法更新动态创建的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17071208/