Strange problems with Android fragmenttransaction in different versions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.