Android Fragment switchover multiple interface overlapping issues hidden issues
Next to the previous blog post: the problem of loading data choppy by switching Android fragment is to hide the previous fragmenthide to solve the choppy problem without having to create a new fragment. Because the left-side suspended menu has more than 10 menus, if you want to hide all the other menus during each menu switch, the code will be very bloated, the previously hidden code is as follows:
/* If (! OpenPositionFragment. isAdded () {// first checks whether transaction has been added. hide (priceFragment ). add (R. id. fragment_container, openPositionFragment ). commit (); // hide the current fragment, and add the following titleView to the Activity. setText (openPositionFragment. getFragmentTitle ();} else {transaction. hide (priceFragment ). show (openPositionFragment ). commit (); // hide the current fragment and display the next titleView. setText (openPositionFragment. getFragmentTitle ());}*/
For click events under each menu button, you must write such code, and only one of them can be hidden. As a result, the switchover may overlap and cannot be correctly displayed.
The solution is:
First hide all fragment, and then enter the click event. If the fragment has been instantiated, show is good. If it has not been instantiated yet, first create a new one, then show, finally, remember commit. I did not write this code, leading to a null pointer exception. Note that the first fragment you enter must be instantiated first. The modified code is as follows:
Private void initOpenMenuItem (View popupWindow_view) {DrawableCenterTextView menu_price = (DrawableCenterTextView) popupWindow_view.findViewById (R. id. menu_price); menu_price.setOnClickListener (new OnClickListener () {FragmentTransaction transaction; @ Overridepublic void onClick (View v) {progressDialog. show (); transaction = manager. beginTransaction (); hideFragments (transaction);/** qiulinhe: December 10, July 21, 2015: 54: 51 solve the problem of choppy switching */if (priceFragment = null) {// if MessageFragment is empty, create and add the priceFragment = new PriceFragment (); transaction. add (R. id. fragment_container, priceFragment); titleView. setText (priceFragment. getFragmentTitle ();} else {// transaction is displayed directly if MessageFragment is not empty. show (priceFragment); titleView. setText (priceFragment. getFragmentTitle ();} transaction. commit (); popupWindow. dismis S (); progressDialog. dismiss () ;}}) ;}/ *** sets all Fragment to hidden. ** @ Param transaction * The transaction used to perform operations on Fragment */private void hideFragments (FragmentTransaction transaction) {if (priceFragment! = Null) {transaction. hide (priceFragment);} if (openPositionFragment! = Null) {transaction. hide (openPositionFragment);} if (closeHisFragment! = Null) {transaction. hide (closeHisFragment);} if (orderHisFragment! = Null) {transaction. hide (orderHisFragment );}}
In this way, the fragment switchover overlaps and cannot be properly displayed. However, the following question is raised: After the hide method is executed, the hidden fragment. if the background is getting data and refreshing the interface, will it cause too much data and program crash?
During background printing, the current fragment update data is indeed displayed, but the hidden fragment is still running in the background, which will inevitably cause more and more memory usage.