傻瓜式的方式来处理方向变化的片段

public class MainActivity extends Activity implements MainMenuFragment.OnMainMenuItemSelectedListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); // add menu fragment MainMenuFragment myFragment = new MainMenuFragment(); fragmentTransaction.add(R.id.menu_fragment, myFragment); //add content DetailPart1 content1= new DetailPart1 (); fragmentTransaction.add(R.id.content_fragment, content1); fragmentTransaction.commit(); } public void onMainMenuSelected(String tag) { //next menu is selected replace existing fragment } 

我需要并排显示两个列表视图,左侧菜单和右侧内容,默认情况下,第一个菜单被选中,其内容显示在右侧。显示内容的片段如下

 public class DetailPart1 extends Fragment { ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>(); ListAdapter adap; ListView listview; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if(savedInstanceState!=null){ myList = (ArrayList)savedInstanceState.getSerializable("MYLIST_obj"); adap = new LoadImageFromArrayListAdapter(getActivity(),myList ); listview.setAdapter(adap); }else{ //get list and load in list view getlistTask = new GetALLListTasks().execute(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.skyview_fragment, container,false); return v; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putSerializable("MYLIST_obj", myList ); } } 

onActivityCreated和onCreateView被调用两次 ,有很多使用片段的例子,因此我是这个片段的初学者,我无法将这个例子与我的问题联系起来。我需要一种更好的方式来处理方向变化。没有声明清单文件中的android:configChanges,我需要的活动销毁和重新创build,以便我可以在横向模式中使用不同的布局。请帮助解决方法

Solutions Collecting From Web of "傻瓜式的方式来处理方向变化的片段"

每当您在活动中closures屏幕时,您正在创build一个新片段onCreate(); 但是你也用super.onCreate(savedInstanceState);维护旧的super.onCreate(savedInstanceState); 。 所以也许设置标签,如果它存在,find片段,或者将null包传递给super。

这花了我一些时间来学习,当你使用viewpager这样的东西的时候,这真的可以成为一个双赢。

我build议你阅读关于这个确切主题的额外时间片段 。

这里是一个如何处理定期的方向变化碎片的例子:

活动

 public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { TestFragment test = new TestFragment(); test.setArguments(getIntent().getExtras()); getSupportFragmentManager().beginTransaction().replace(android.R.id.content, test, "your_fragment_tag").commit(); } else { TestFragment test = (TestFragment) getSupportFragmentManager().findFragmentByTag("your_fragment_tag"); } } } 

片段

 public class TestFragment extends Fragment { public static final String KEY_ITEM = "unique_key"; public static final String KEY_INDEX = "index_key"; private String mTime; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_layout, container, false); if (savedInstanceState != null) { // Restore last state mTime = savedInstanceState.getString("time_key"); } else { mTime = "" + Calendar.getInstance().getTimeInMillis(); } TextView title = (TextView) view.findViewById(R.id.fragment_test); title.setText(mTime); return view; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("time_key", mTime); } } 

关于如何保留方向变化和活动重新创build数据的一个很好的指导可以在android指南中find。

概要:

  1. 使您的片段保持不变:

     setRetainInstance(true); 
  2. 只在必要时创build一个新的片段(或者至less从中获取数据)

     dataFragment = (DataFragment) fm.findFragmentByTag("data"); // create the fragment and data the first time if (dataFragment == null) {