I have a MainActivity that loads a fragment in onCreate. I have another fragment that I load from the action bar. If I change the screen orientation on the first fragment, it works as expected. If I change on the second one it resets to the first fragment. This appears to be because it rebuilds everything and it puts the original fragment back into the fragment manager. I am certain there is a way to keep track of the currently displayed fragment, but I'm not sure how.
@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);
}
I'm sure this must be something that has been done a bunch, but I am struggling to know how to search for it.