At present, an app that uses a fragment switch in a layout block, where a button presses the following event:
Btntranslate.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {/* The following actions are performed when the current interface is not a translation interface */if (main_fragment_id! = Currentfragmentid) {setchoosedbtnstate (main_fragment_id); Fragmentmanager Fragmentmanager = Getfragmentmanager (); Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction (); if (null = = Fragmenttranslate) Fragmenttranslate = new Mainui (); hidecurrentfragment (fragmenttransaction); Fragmenttransaction.show ( fragmenttranslate); <pre name= "code" class= "java" > fragmenttransaction.add (r.id.fragment_content, Fragmenttranslate, "Mainui");
Fragmenttransaction.commit (); currentfragmentid = main_fragment_id;}});
Hidecurrentfragment (fragmenttransaction); Fragmenttransaction.show (fragmenttranslate);
These two sentences are intended to improve the switching speed, and save the user generated data and choose Hide/Show instead of replace (Remove/add), the runtime in the Android 4.0 simulator and my Android 4.1 mobile Phone No problem, the effect is very good, But when I put the program on my Android 4.4 (Galaxy S5 It seems that the problem of this model is very many ... ) When the Flash comes back! The case is that when I click on a button to create an instance of fragment, the program runs normally, but when I click on the interface button that has already created the instance again, it will go away, I don't know what is going on, and the API has not found the relevant instructions.
For a long time, and finally saw
<pre name= "code" class= "Java" >fragmenttransaction.add (R.id.fragment_content, Fragmenttranslate, "MainUI");
This sentence, its role is to add the resulting fragment instance to the specified layout block, and then Lenovo my problem, that is, join once and then join the problem, so try to only create fragment instance when the first call this statement, sure enough problem solved!
The modified code is as follows:
Btntranslate.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {/* The following actions are performed when the current interface is not a translation interface */if (main_fragment_id! = Currentfragmentid) {setchoosedbtnstate (main_fragment_id); Fragmentmanager Fragmentmanager = Getfragmentmanager (); Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction (); if (null = = Fragmenttranslate) { Fragmenttranslate = new Mainui (); Fragmenttransaction.add (R.id.fragment_content, Fragmenttranslate, "MainUI");} Hidecurrentfragment (fragmenttransaction); Fragmenttransaction.show (fragmenttranslate); Fragmenttransaction.commit (); currentfragmentid = main_fragment_id;}});
Strange problems with Android fragmenttransaction in different versions