Radiogroup+fragment is a very common home navigation control, replacing fragment with replace (), which was used for easy code, as follows:
Getsupportfragmentmanager (). BeginTransaction (). replace (r.id.framlayout,fragment). commit ();
This method is convenient to use, but the biggest problem is that each execution clears all the fragment objects in the previous fragment queue, and then reloads a new fragment in,
Equivalent to using remove () + add ();
This causes the page to reload every time the page is toggled, and if you have a time-consuming operation like a network request, it's a waste of resources.
So we studied, using the Add () + Show () + Hide () Three methods used in combination.
Using these three methods you will find that the Oncreateview () method is no longer executed when the second switchover occurs. The core code is as follows:
1Fragment = Manager.findfragmentbytag ("First");2 if(Fragment = =NULL) {3Fragment =Newhomefragment ();4Logutils.loginfostar ("New Once");5Transaction.add (R.id.framlayout, Fragment, "first"). commit ();6}Else if(fragment.isadded ()) {7 Transaction.show (fragment). commit ();8Logutils.loginfostar ("Zhixingzzh");9}Else{TenTransaction.add (R.id.framlayout, Fragment, "first"). commit (); One}
if (prefragment! = null)
Transaction.hide (prefragment); Hide previous Fragment
prefragment = fragment;//Record previous fragment
The full code is as follows:
1Radiogroup.setoncheckedchangelistener (NewRadiogroup.oncheckedchangelistener () {2 @Override3 Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) {4Fragmentmanager Manager =Getsupportfragmentmanager ();5Fragmenttransaction transaction =manager.begintransaction ();6Fragment Fragment =NULL;7 Switch(checkedid) {8 CaseR.id.radio_first:9 TenFragment = Manager.findfragmentbytag ("First"); One if(Fragment = =NULL) { AFragment =Newhomefragment (); -Logutils.loginfostar ("New Once"); -Transaction.add (R.id.framlayout, Fragment, "first"). commit (); the}Else if(fragment.isadded ()) { - Transaction.show (fragment). commit (); -Logutils.loginfostar ("Zhixingzzh"); -}Else{ +Transaction.add (R.id.framlayout, Fragment, "first"). commit (); - } + A Break; at CaseR.id.radio_second: -Fragment=manager.findfragmentbytag ("Second"); - if(fragment==NULL){ -Fragment =Newcategoryfragment (); -Transaction.add (R.id.framlayout,fragment, "second"). commit (); -}Else if(fragment.isadded ()) { in Transaction.show (fragment). commit (); -}Else{ toTransaction.add (R.id.framlayout,fragment, "second"). commit (); + - } the Break; * CaseR.id.radio_third: $Fragment=manager.findfragmentbytag ("Third");Panax Notoginseng if(fragment==NULL){ -Fragment =Newsearchfragment (); theTransaction.add (R.id.framlayout,fragment, "third"). commit (); +}Else if(fragment.isadded ()) { A Transaction.show (fragment). commit (); the}Else{ +Transaction.add (R.id.framlayout,fragment, "third"). commit (); - } $ Break; $ CaseR.id.radio_fourth: - -Fragment=manager.findfragmentbytag ("Fourth"); the if(fragment==NULL){ -Fragment =Newshoppingcartfragment ();WuyiTransaction.add (R.id.framlayout,fragment, "fourth"). commit (); the}Else if(fragment.isadded ()) { - Transaction.show (fragment). commit (); Wu}Else{ -Transaction.add (R.id.framlayout,fragment, "fourth"). commit (); About } $ Break; - Caser.id.radio_fifth: -Fragment=manager.findfragmentbytag ("Fifth"); - if(fragment==NULL){ AFragment =Newmorefragment (); +Transaction.add (R.id.framlayout,fragment, "fifth"). commit (); the}Else if(fragment.isadded ()) { - Transaction.show (fragment). commit (); $}Else{ theTransaction.add (R.id.framlayout,fragment, "fifth"). commit (); the } the Break; the } - if(Prefragment! =NULL) in transaction.hide (prefragment); thePrefragment =fragment; the } About }); the //by setting the default check in this way, the listening method executes two times, that is, the first home page fragment will be created two times. the //Radiogroup.check (r.id.radio_first); the //Use this method to set the default selected page Change Listening method will only be executed once +Radiofirst.setchecked (true);
A detailed explanation of how to perform two listening methods when initializing a page is the default way to select the homepage, which can be easily understood in the following blog post:
http://blog.csdn.net/piglovesula/article/details/9820521< Span style= "color: #cc7832;" >< Span style= "color: #cc7832;" >
/span>
Radiogroup+fragment uses the Add () method of Fragment to prevent the use of replace to reload the page every time, resulting in wasted resources