Fragment status switch and fragment status Switch

Source: Internet
Author: User

Fragment status switch and fragment status Switch

When you use Activity to manage multiple Fragment, replace is used for each Fragment switchover. As a result, xxx is not currently in the FragmentManager fails.

On the Internet, if replace is used for switching, the switched Fragment will be replaced, and the switched Fragment view will be deleted from the viewtree. As a result, the Fragment will be re-created every time during the switching process.

Each time you use add to add Fragment, show is used when the switchover is made, and the switched Fragment is hidden with hide. The Code is as follows:

1/** 2 * Fragment jump 3 * @ param fm 4 * @ param fragmentClass 5 * @ param tag 6 * @ param args 7 */8 public void turnToFragment (Class <? Extends Fragment> fromFragmentClass, Class <? Extends Fragment> toFragmentClass, Bundle args) {9 10 FragmentManager fm = getSupportFragmentManager (); 11 // The switched Fragment tag 12 String fromTag = fromFragmentClass. getSimpleName (); 13 // The switched Fragment tag 14 String toTag = toFragmentClass. getSimpleName (); 15 // find the switched Fragment16 Fragment fromFragment = fm. findFragmentByTag (fromTag); 17 Fragment toFragment = fm. findFragmentByTag (toTag); 18 // If the Fragment to be switched does not exist, Create 19 if (toFragment = null) {20 try {21 toFragment = toFragmentClass. newInstance (); 22 toFragment. setArguments (args); 23} catch (java. lang. instantiationException e) {24 e. printStackTrace (); 25} catch (IllegalAccessException e) {26 e. printStackTrace (); 27} 28} 29 // if any parameter is passed, 30 if (args! = Null &&! Args. isEmpty () {31 toFragment. getArguments (). putAll (args); 32} 33 // Fragment transaction 34 FragmentTransaction ft = fm. beginTransaction (); 35 // sets the Fragment switching effect 36 ft. setCustomAnimations (android. r. anim. fade_in, android. r. anim. fade_out, 37 android. r. anim. fade_in, android. r. anim. fade_out); 38/** 39 * If the Fragment to be switched is not added by the Fragment transaction, the switched Fragment is hidden and the Fragment40 * to be switched is added. Otherwise, the switched Fragment is hidden, and the Fragment41 */42 if (! ToFragment. isAdded () {43 ft. hide (fromFragment ). add (R. id. content_frame, toFragment, toTag); 44} else {45 ft. hide (fromFragment ). show (toFragment); 46} 47 // Add to return stack 48 // ft. addToBackStack (tag); 49 // commit the transaction in the unretained state 50 ft. commitAllowingStateLoss (); 51}

Here is a FragmentTransaction (transaction), which is the core class of Fragment switching. It has two methods: replace, add, replace Fragment, and add Fragment. The difference between the two is: replace = remove old fragment, add new fragment,

When our application (that is, the Activity that manages multiple Fragment) runs to the background (that is, the current screen is launched), The onPause () method of the Activity is triggered, while the onPause () method of the Activity is () it will call the same method of Fragment managed by it, but when I use replace, the original Fragment has been removed, so when I call the principle of Fragment's onPause () the preceding xxx is not currently in the FragmentManager exception occurs when the method is not crude. debug is used to verify that:


How to save the status when switching android Fragment to avoid repeated calls to onCreateView ()

In the sdk sample APIdemo's FragmentHideShow. java

How can I keep the status of actionbar + Fragment switching back and forth?

After two fragment clicks are generated, the content in the textview is random after the result is switched back and forth? Why do I call creat once? Please answer!

Related Article

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.