Activity+fragment multiple switch page blank issue

Source: Internet
Author: User

Just get started with a project lazy with viewpager+fragment mode, although in the later projects still use Viewpager+fragment. First talk about the problem, a number of times to switch fragment when the page appears blank, just beginning to think that the parameters passed the problem, but also did appear this problem, hand-cheap caused.

For example, according to the incoming tag instantiation, mistakenly put the first data and the second pass the same data. Copy of the wrong, dead and alive can not switch, and then debug to fix:

/**
* Initialize the Newinstance management fragment page and set the current fragment page
*/
public static basefragment newinstance (Context context,string tag) {
Basefragment basefragment = null;
if (textutils.equals (tag, context.getstring (R.STRING.M))) {
if (basefragment==null)
Basefragment = new Fragmenta ();
}else if (textutils.equals (tag, context.getstring (R.STRING.F))) {
if (basefragment==null)
Basefragment = new Fragmentb ();
}else if (textutils.equals (tag, context.getstring (R.STRING.G))) {
if (basefragment==null)
Basefragment = new Fragmentc ();
}
return basefragment;

}

=================================

Later, the problem manifested in, the first fragment load data slowly in the not loaded when the time to switch other pages when the blank, in the Internet to find a variety of methods just started to think that the cache did not do a good job (did not have time to do the optimization of the cache), and later read a variety of posts, Set the manifest file to Signtask, ... Finally realized that the fragment state was not saved ...

Just add a judgment to the Oncreateview and take care of it:

@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
if (MView! = null) {
ViewGroup parent = (ViewGroup) mview.getparent ();
if (parent! = NULL) {
Parent.removeview (MView);
}
return mView;
}
View Rootview = null;
Rootview = Inflater.inflate (R.layout.question, container, false);
MView = Rootview;
return rootview;
}

Activity+fragment multiple switch page blank issue

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.