popBackStack controls the fragment refresh problem

   After using popBackStack() to return to the previous fragment, it is found that the fragment will be reloaded and

   checked to find that the loading method is written in onViewCreated, and can be changed to onCreate


   ============= After a few moments The dividing line is coming again =====================

   The above method is not very good, another solution:

   transaction.add replaces transaction.replace on

   the official website It is said that .replace() == remove().add()

   so that you can control the return to the previous fragment as you like. The problem of reloading


   ============= It came again after a few days The dividing line =======================

   The above solution is only suitable for general, more complex application scenarios
   with fragment A, B, C three

   A is a static fragment, B is a dynamic fragment that stores a data list, and C is a fragment that adds a new record.

   If A --> B uses replace, and B-->C uses add, then it is not done on C. When backing directly, it will clean up both B and C, because ADD, B and C are tied together (or occupy the same one at the same time) to

   control the automatic refresh of the original page:
   When B-->C

   transaction.hidden(B);
   transaction.add(R.id.content, C, C.getClass().getName());
   transaction.addToBackStack(C.getClass().getName());
   transaction.commit();


  At the same time, an overridden method is added in B
/**
 * The first time you come in will not trigger
 * Triggered when jumping to the next page: true
 * will fire on return: false
 * Does not fire when returning to the previous level
 * @param hidden
   */
@Override
public void onHiddenChanged(boolean hidden) {
	super.onHiddenChanged(hidden);
	LogTool.debug("The B hidden is :"+hidden);
	if (!hidden) { //When returning
		refresh(); //call method to refresh
	}
}	



Of course, there are other ways: C defines interface B to call and complete the refresh.

   ============= After a few days, the dividing line comes again======== ==============

   When using add fragment, sometimes click on the current fragment, the click effect will penetrate the current fragment, and the fragment hidden below will be corresponding, the most common The method is to add background color and clickable under the root layout in the view corresponding to each fragment
    android:background="@drawable/bg"
    android:clickable="true"


However, I found that the following situation cannot be handled by the above method. I customized a view in the fragment, similar to the toolbar function, with two buttons on the left and right. The VIEW of the first fragment has two buttons on the left and right, and the one at the top position There is only one button on the left or right in the VIEW in the fragment. At this time, when you click the button on the side, the click will be acquired by the hidden fragment. You

need to use the following method
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        parent = inflater.inflate(R.layout.fragment_corp_advanced_detail, container, false);

        //Prevent penetration by click
        parent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });

        return parent;
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327046140&siteId=291194637