FragmentTransaction is not allowed to be added to back stack?

前端 未结 4 1976
迷失自我
迷失自我 2021-01-22 21:18

why cant my fragments be added to back stack?

@Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        FragmentTransaction          


        
4条回答
  •  孤城傲影
    2021-01-22 22:00

    The only problem in your code is that you 'correctly' create a transaction, but then you never use it. The supplied transaction FragmentTransaction ft cannot be added to the backstack because it is not supported by the listener, however this should work:

    @Override
        public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
    
                ListFragment newListFragment = new ListFragment();
                Fragment newFragment = new EntryFrag();
    
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.frameOne, newListFragment);
                transaction.replace(R.id.frameTwo, newFragment);
    
    
                transaction.addToBackStack(null);
                transaction.commit();
        }
    

    Note the use of transaction.replace(... vice ft.replace(... also you must commit() this yourself, as you properly did, because TabListener will only auto-commit FragmentTransaction ft

    Also, you can read about adding to the back stack not being supported in the TabListener API docs here: http://developer.android.com/reference/android/app/ActionBar.TabListener.html

提交回复
热议问题