Two layers of Fragment nesting. When the outer Fragment is switched, the Fragment layer does not display content, and the fragment layer is nested.

Source: Internet
Author: User

Two layers of Fragment nesting. When the outer Fragment is switched, the Fragment layer does not display content, and the fragment layer is nested.

Respect others' labor results, reprinted please explain the source: http://blog.csdn.net/bingospunky/article/details/46847269

Requirement

There is such a requirement on the build interface: two layers of Fragment nesting are required, the inner layer needs to slide the switching effect, and the outer interface does not need to slide the effect. The inner layer uses ViewPager to switch, and the outer layer uses replace to switch. The interface created in this way cannot achieve the desired effect. The Fragment of the inner layer can only display the content for the first time. When the outer Fragment is switched, The Fragent of the inner layer is empty.

I try to use replace switch for both Fragment layers to achieve the effect. However, the project requires a sliding switch on the inner layer. In this case, you can slightly modify the requirement. The requirement for canceling the Inner Sliding effect is OK. But as a person who can solve this problem, I still want to try to achieve the Inner Sliding effect. How can this problem be achieved?

Show/hide

Replace is used when switching the outer Fragment. The lifecycle function of the switched Fragment is executed to onDestroy. I guess the Fragment in the inner layer is empty because it is switched to Fragment. When the onDestroy function is executed, FragmentManager is implemented with some ulterior motives. We can choose not to perform onDestroy switching, so we can select show/hide to switch the outer Fragment. This method is feasible, but it will lead to a new problem.

After a process is killed

For example, if four Fragment entries have been added to an activity, we can use show/hide to switch between the two. If you press the home Key to return to the desktop at this time, the process of this application will be killed by the system for memory reasons later, and then we press the home Key to return to the application, then the system will create a new activity for us. In the new activity created by the system, we cannot properly restore the four Fragment that we just added. There will still be four Fragment in the interface (if the Fragment does not have a background color, then the four Fragment will overlap), and the switchover event does not respond. So how can we solve this problem? First, we need to fix this problem. When we add multiple Fragment to the activity and the application is killed by the system and re-created, multiple Fragment instances will have an exception. When this happens, you can finish the activity. This is rare, so you can finish the activity.

Last

This article only introduces the problem of two-layer Fragment nesting and the problem surface. I also tried to read the Fragment source code just now, but there is no progress. In the next time, I will read the source code and find the above two questions (1. why is the internal Fragment not displayed when the two-layer Fragment is switched? 2. The activity is killed by the system. Why does the Fragmant overlap and does not respond to the switchover ?) The problem is solved from the source code point.

Last

In a twinkling of an eye, it's already half past four a.m., and the half past four AM is very quiet and beautiful.



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.