Getting the error “Java.lang.IllegalStateException Activity has been destroyed” when using tabs with ViewPager

前端 未结 13 1894
再見小時候
再見小時候 2020-11-22 11:53

I have an application that consists of using ActionBarSherlock in tab mode.I have 5 tabs and the content of each tab is handled using fragments. For tab2 though, I have a fr

相关标签:
13条回答
  • 2020-11-22 12:46

    I know this is an old post, but the suggested answers didn't work on my end. I want to leave this here just in case someone will find it useful.

    What i did is:

    @Override
    public void onResume() {
        super.onResume();
        // add all fragments
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        for(Fragment fragment : fragmentPages){
            String tag = fragment.getClass().getSimpleName();
            fragmentTransaction.add(R.id.contentPanel, fragment, tag);
            if(fragmentPages.indexOf(fragment) != currentPosition){
                fragmentTransaction.hide(fragment);
            } else {
                lastTag = tag;
            }
        }
        fragmentTransaction.commit();
    }
    

    Then in:

    @Override
    public void onPause() {
        super.onPause();
        // remove all attached fragments
        for(Fragment fragment: fragmentPages){
            getChildFragmentManager().beginTransaction().remove(fragment).commit();
        }
    }
    
    0 讨论(0)
提交回复
热议问题