Android的 - 对方向变化的动态片段问题 [英] Android - Dynamic Fragment problems on orientation change
问题描述
我在使用动态片段的问题。如果我不改变方向,它做工精细。当我改变方向,我点击ListView项。它不会改变TextView的。
I'm having a problem with dynamic fragment . If I'm not change orientation , it work fine . When I change orientation , I click on ListView item . It's not change textview .
这是DynamicActivity类
This is DynamicActivity class
public class DynamicActivity extends Activity implements FragmentCoordinator{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
ListContentFragment listContentFragment = new ListContentFragment();
DetailsContentFragment detailsContentFragment = new DetailsContentFragment();
transaction.add(R.id.listContainer, listContentFragment,"listContent");
transaction.add(R.id.detailsContainer, detailsContentFragment,"detailsContent");
transaction.commit();
}
@Override
public void onSetContentDetails(int index) {
FragmentManager fragmentManager = getFragmentManager();
DetailsContentFragment detailsContentFragment = (DetailsContentFragment)fragmentManager.findFragmentByTag("detailsContent");
detailsContentFragment.setContentDetails(index);
}
}
和DetailsContentFragment类
And DetailsContentFragment class
public class DetailsContentFragment extends Fragment {
TextView lbMess;
String[] array;
int saveIndex;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.details_content_fragment, container,false);
array = getResources().getStringArray(R.array.list_details);
lbMess = (TextView)v.findViewById(R.id.lbDetails);
int currentIndex = savedInstanceState == null ? 0 : savedInstanceState.getInt("indexContent",0);
setContentDetails(currentIndex);
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("indexContent", saveIndex);
}
public void setContentDetails(int index) {
saveIndex = index;
lbMess.setText(array[saveIndex]);
}
}
予有调试,但它不具有任何错误。请给我一些建议
I have debug but it doesn't have any error . Please give me some advice
推荐答案
我发现的问题有: 当系统破坏,并自动重新创建,因为运行时配置更改的活动,该活动 重新实例化现有的片段。
I found the problems are : When the system destroys and re-creates an activity because of a run-time configuration change, the activity automatically re-instantiates existing fragments.
这是不适合的活动的布局申报的静态的片段中的问题。
This isn’t a problem for "static" fragments declared in the activity’s layout.
但对于动态的片段,我需要测试这种情况prevent创建我片段的第二个实例。
But for "dynamic" fragments, i need to test for this situation to prevent creating a second instance of my fragment.
我检查传递给我的活动的onCreate()的捆绑参数为null。
I check the Bundle argument passed to my activity’s onCreate() is null.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
if(savedInstanceState == null)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
ListContentFragment listContentFragment = new ListContentFragment();
DetailsContentFragment detailsContentFragment = new DetailsContentFragment();
transaction.add(R.id.listContainer, listContentFragment,"listContent");
transaction.add(R.id.detailsContainer, detailsContentFragment,"detailsContent");
transaction.commit();
}
}
和它做工精细。我认为是有人有同样的问题有所帮助。
And it work fine . I think is helpful for someone have same problems .
这篇关于Android的 - 对方向变化的动态片段问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!