Working with a fragment and a viewing pager: cannot change a tag of a fragment AND Recursive record to executePendingTransactions

Firstly, I know that these topics were created a lot of time on stackoverflow, but I did not find a solution to my problems. Secondly, I'm French, so my English is not perfect, sorry for the advance payment and tell me if you do not understand something. And to finish this introduction, this is the first time I'm dealing with fragments, so sorry if there is something that I don’t understand very well!

I have three buttons that allow you to switch between the three fragments. Inside one of these fragments, I have a pager with two fragments. At the moment, each fragment (there are 5) contains only a TextView. I use the latest version of android-support-v4 (I read a lot of articles on stackoverflow that say that the latest version of support resolves the error "Recursive write to execute PendingTransactions").

My two problems:

  • When I double-click with one button, I have an IllegaleStateException "cannot change fragment tag". I was able to fix this by creating a new fragment using the onButtonSelected method, but I don’t want to recreate the fragment every time, for memory and functional reasons: the fragment must maintain its state. This problem is not my main problem, indeed, I know that it is possible to disable the button when the user is already on the fragment, but it is strange to have an exception when this control is not performed, no ?.

  • , , IllegalStateException " PendingTransactions". , FragmentStatePagerAdapter FragmentPageAdapter (. ), crash, , !

?

Java , , , .

MainActivity:

public class MainActivity extends SherlockFragmentActivity{

private SherlockFragment fragmentOne, fragmentTwo, fragmentThree;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//      this.fragmentOne = new fragmentOne();
//      this.fragmentTwo = new fragmentTwo();
//      this.fragmentThree = new fragmentThree();
// Call new or instanciate ? What the correct way ?

    this.fragmentOne = (SherlockFragment) SherlockFragment.instantiate(this, FragmentOne.class.getName());
    this.fragmentTwo = (SherlockFragment) SherlockFragment.instantiate(this, FragmentTwo.class.getName());
    this.fragmentThree = (SherlockFragment) SherlockFragment.instantiate(this, FragmentThree.class.getName());

    // Add fragment
    FragmentTransaction transaction = (
        this.getSupportFragmentManager().beginTransaction()
    );      
    transaction.add(
        R.id.tab_fragment,
        this.fragmentOne,
        this.fragmentOne.toString()
    );
    transaction.commit();

}

public void onButtonSelected(View v){

    switch (v.getId()){

        case R.id.button_one_tab:{
            showFragment(this.fragmentThree);
            break;
        }
        case R.id.button_two_tab:{
            showFragment(this.fragmentOne);
            break;
        }
        case R.id.button_three_tab:{
            showFragment(this.fragmentTwo);
            break;
        }
        default:{
            break;
        }

    }

}

public void showFragment(SherlockFragment fragmentToShow){

    FragmentTransaction transaction = (
        this.getSupportFragmentManager().beginTransaction()
    );

    transaction.replace(R.id.tab_fragment, fragmentToShow, fragmentToShow.toString());
    transaction.commit();

}

}

, TextView. ( , DirectionalViewPager - lib - ViewPager):

public class FragmentOne extends SherlockFragment{

private FragmentOneAdapter fragmentOneAdapter;

@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    // Set up the pager
    final DirectionalViewPager pager = (DirectionalViewPager)getActivity().findViewById(R.id.pager);
    if (this.fragmentOneAdapter== null){
        this.fragmentOneAdapter= new FragmentOneAdapter (getActivity().getSupportFragmentManager());
    }
    pager.setAdapter(fragmentOneAdapter);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_landing_page, container, false);
}

FragmentOneAdapter:

public class FragmentOneAdapter extends FragmentPagerAdapter{

private ArrayList<SherlockFragment> fragmentsList;

public FragmentOneAdapter(FragmentManager fragmentManager) {

    super(fragmentManager);

    SherlockFragment fragmentFour = new FragmentFour();
    SherlockFragment fragmentFive = new FragmentFive();

    this.fragmentsList = new ArrayList<SherlockFragment>();
    this.fragmentsList.add(fragmentFour);
    this.fragmentsList.add(fragmentFive);

}

@Override
public Fragment getItem(int position) {
    return this.fragmentsList.get(position);
}

@Override
public int getCount() {
    return this.fragmentsList.size();
}

}

!

+5
1

!

getSupportFragmentManager() FragmentOne getChildFragmentManager(). onButtonSelected, ( , : java.lang.IllegalStateException: Activity has been destroyed).

: , FragmentOne, fragmentTwo fragmentThree. ?

+3

All Articles