S
S
samarjan2016-06-06 11:22:52
Android
samarjan, 2016-06-06 11:22:52

How to control the simultaneous launch of fragments on click on a list item?

There is a fragment with a list, by clicking on an element of the list, a new fragment is created, and it is added to the FragmentManager, the fragment with the list is displayed and hidden. standard task. The problem is that if you click on two list items at the same time, then two fragments will be created and placed in the FragmentManager and displayed at the same time, and it is desirable that only one fragment be created.
executePendingTransactions() does not help
Now implemented with a trigger as a boolean variable that skips the click event if it has already been clicked, but until the fragment called by clicking on the list item is destroyed. Information about the destruction of a detailed fragment is passed through an interface that implements a fragment with a list.
Is it possible to do it somehow differently?

public class ListFragment extends Fragment implements DetailFragmentFragment.onDetailFragmentLifeCycleListener {
  private boolean isTransactionPerforming = false;

  ...
  @Override
  public void onClick(Data someData) {
    if (!isTransactionPerforming) {
      isTransactionPerforming = true;
    } else {
      return;
    }
    DetailFragment DetailFragment = new DetailFragment();
    DetailFragment.setMyData(someData);
    DetailFragment.setFragmentVisible();
    DetailFragment.setDetailLifeCycleListener(this);
    getActivity().getSupportFragmentManager()
        .beginTransaction()
        .hide(currntListFragment)
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE)
        .add(R.id.List_frameLayout, DetailFragment)
        .addToBackStack("details")
        .commitAllowingStateLoss();
  }

  @Override
  public void onShown() {
    isTransactionPerforming = false;
  }
  ...
}

public class DetailFragment extends Fragment {

  ...

  @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDetailLifeCycleListener != null)
            mDetailLifeCycleListener.onShown();
    }

    ...

    public onDetailLifeCycleListener mDetailLifeCycleListener;

    public void setDetailLifeCycleListener(onDetailLifeCycleListener mDetailLifeCycleListener) {
        this.mDetailLifeCycleListener = mDetailLifeCycleListener;
    }

    public interface onDetailLifeCycleListener {
        public void onShown();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IceJOKER, 2016-06-06
@samarjan

make the list CHOICE_MODE SINGLE?
call replace instead of add on the manager fragment?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question