I have a drop down list of actions in the taskbar. The problem is that when I switch to another fragment and then change the orientation, it puts the first fragment inside, although I think I pass savedInstanceState correctly. The problem is that onNavigationItemSelected gets called, so ... how would I handle this correctly? I can make the savedInstanceState variable a field, but this is just wrong ...
public class MainActivity extends FragmentActivity implements MyListFragment.OnArticleSelectedListener { public static final String TAG = "MainActivity"; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(savedInstanceState != null) { Fragment savedFragment = getSupportFragmentManager().getFragment(savedInstanceState, "saved_fragment"); Log.d(MainActivity.TAG, "savedInstanceState != null: " + savedFragment.getTag()); getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_container, savedFragment, savedFragment.getTag()) .commit(); } else { Log.d(MainActivity.TAG, "savedInstanceState == null"); getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_container, new MyListFragment(), MyListFragment.TAG) .commit(); } ActionBar actionBar = getActionBar(); actionBar.setDisplayShowTitleEnabled(false); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); String[] array = new String[] { "InzerΓ‘ty", "Dummy frag" }; SpinnerAdapter mSpinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, array); actionBar.setListNavigationCallbacks(mSpinnerAdapter, new ActionBar.OnNavigationListener() { @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { Log.d(MainActivity.TAG, "onNavitagionItemSelected"); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); switch(itemPosition) { case 0: transaction.replace(R.id.fragment_container, new MyListFragment(), MyListFragment.TAG); break; case 1: transaction.replace(R.id.fragment_container, new MyDummyFragment(), MyDummyFragment.TAG); break; } transaction.commit(); return true; } }); } @Override public void onArticleSelected(Bundle bundle) { Log.d(MainActivity.TAG, "MainActivity # onArticleSelected"); Intent intent = new Intent(this, DetailActivity.class); intent.putExtras(bundle); startActivity(intent); } @Override protected void onSaveInstanceState(Bundle outState) { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); Log.d(MainActivity.TAG, "MainActivity # onSaveInstanceState: " + currentFragment.getTag()); getSupportFragmentManager().putFragment(outState, "saved_fragment", currentFragment); super.onSaveInstanceState(outState); }
}
urSus source share