我有一个MainActivity,它在onCreate中加载一个片段。我还有另一个片段,在操作栏中加载。如果我在第一个片段上更改屏幕方向,它按预期工作。如果我在第二个片段上更改,则重置为第一个片段。这似乎是因为它重新构建了一切,并将原始片段放回片段管理器。我确信有一种方法可以跟踪当前显示的片段,但我不确定该如何做。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Database entries
myDataSource = new FillupDataSource(this);
myDataSource.open();
// Create an instance of the MyListFragment
MyListFragment firstFragment = new MyListFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("dataSource", (Serializable) myDataSource);
firstFragment.setArguments(bundle);
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
getActionBar().setDisplayHomeAsUpEnabled(true);
}
我相信这一定是已经做过很多次的事情,但是我不知道该如何搜索。