Separate Back Stack for each tab in Android using Fragments

后端 未结 12 1016
南旧
南旧 2020-11-22 14:51

I\'m trying to implement tabs for navigation in an Android app. Since TabActivity and ActivityGroup are deprecated I would like to implement it using Fragments instead.

12条回答
  •  耶瑟儿~
    2020-11-22 15:20

    This thread was very very interesting and useful.
    Thanks Krishnabhadra for your explanation and code, I use your code and improved a bit, allowing to persist the stacks, currentTab, etc... from change configuration (rotating mainly).
    Tested on a real 4.0.4 and 2.3.6 devices, not tested on emulator

    I change this part of code on "AppMainTabActivity.java", the rest stay the same. Maybe Krishnabhadra will add this on his code.

    Recover data onCreate:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_main_tab_fragment_layout);
    
        /*  
         *  Navigation stacks for each tab gets created..
         *  tab identifier is used as key to get respective stack for each tab
         */
    
      //if we are recreating this activity...
        if (savedInstanceState!=null) {
             mStacks = (HashMap>) savedInstanceState.get("stack");
             mCurrentTab = savedInstanceState.getString("currentTab");
        }
        else {
        mStacks = new HashMap>();
        mStacks.put(AppConstants.TAB_A, new Stack());
        mStacks.put(AppConstants.TAB_B, new Stack());
    
        }
    
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
    
        initializeTabs();
    
      //set the listener the last, to avoid overwrite mCurrentTab everytime we add a new Tab
        mTabHost.setOnTabChangedListener(listener);
    }
    

    Save the variables and put to Bundle:

     //Save variables while recreating
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("stack", mStacks);
        outState.putString("currentTab", mCurrentTab);
        //outState.putInt("tabHost",mTabHost);
    }
    

    If exist a previous CurrentTab, set this, else create a new Tab_A:

    public void initializeTabs(){
        /* Setup your tab icons and content views.. Nothing special in this..*/
        TabHost.TabSpec spec    =   mTabHost.newTabSpec(AppConstants.TAB_A);
    
        spec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                return findViewById(R.id.realtabcontent);
            }
        });
        spec.setIndicator(createTabView(R.drawable.tab_a_state_btn));
        mTabHost.addTab(spec);
    
    
        spec                    =   mTabHost.newTabSpec(AppConstants.TAB_B);
        spec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                return findViewById(R.id.realtabcontent);
            }
        });
        spec.setIndicator(createTabView(R.drawable.tab_b_state_btn));
        mTabHost.addTab(spec);
    
    //if we have non default Tab as current, change it
        if (mCurrentTab!=null) {
            mTabHost.setCurrentTabByTag(mCurrentTab);
        } else {
            mCurrentTab=AppConstants.TAB_A;
            pushFragments(AppConstants.TAB_A, new AppTabAFirstFragment(), false,true);
        }
    }
    

    I hope this helps other people.

提交回复
热议问题