Fragment Screen Switching problem

Source: Internet
Author: User

By default, when a portrait-to-landscape switch occurs, The fragment in the current activity is regenerated through fragment.instantiate, which uses the default constructor to generate the corresponding fragment, so there will be an error if there is no default constructor, for example:

class myfragment { public myfragment (int  title, String message) {} Public Staticfinal myfragment newinstance (int  title, String message) {    new Myfragment (int  title, String message);     return f;}}
At this time when the screen switch with the myfragment there is no default constructor, will throw the java.lang.InstantiationException exception, the correct way for the use of bundles to pass the parameter modification is as follows:

classmyfragment {Private Static FinalString title= "TITLE", message= "MESSAGE";PrivateString title,message; Public voidonCreate (Bundle saveinstancestate) {title=getarguments (). getString (TITLE); Message=getarguments (). getString (MESSAGE);} Public Static FinalMyfragment newinstance (inttitle, String message) {myfragment F=NewMyfragment (inttitle, String message); Bundle BDL=NewBundle (2);    Bdl.setstring (Title,title);    Bdl.setstring (Message,message);    F.setarguments (BDL); returnf;}}

The fragment created in this way can still get the previously set data through getarguments while switching between the screen and the The principle is that the Onretainnonconfigurationinstance method is called when fragmentactivity switches to save all fragment and their state data (including bundles of this setting) that are managed in Fragmentmanager in a The Fragmentactivity.nonconfigurationinstances object instance, which can be used in the OnCreate method when the new fragmentactivity is started Activity.getlastnonconfigurationi Nstance () method to get the object and then restore all fragment and its state through the Fragmentmanager.restoreallstate method, It is important to note that in this case, the previous fragment is not detach while the active state causes the fragment view to be generated, which can cause two fragment views to overlap. In the official Support.v4 example, FragmentTabs.TabManager.addTab has a piece of code to check whether fragment detach is used to solve this problem.

Through the above processing basic is no problem, but because by default, the entire fragmentactivity will be destroyed and rebuilt, all fragment member variables will be lost, but all fragment state data as described above will be preserved and restored, All views will be recreated at this time.

Workaround One: Add the android:configchanges= "Orientation|keyboardhidden" setting in the corresponding activity configuration so that the fragmentactivity is not destroyed when switching The status and view of all fragment will also be maintained.

Workaround two: Set the third tag parameter when adding fragment using the Fragmenttransaction.add () method, which is then available through Fragmentmanager.findfragmentbytag () at restore time method to retrieve the restored fragment.

Fragment Screen Switching problem

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.