Android _ About fragment switch reload solution share to everyone

Source: Internet
Author: User

The fragment switch is required in the project and is always replaced with the replace () method fragment
However, this will have a problem that should be met by many friends:
Each time you switch, fragment will re-instantiate, that is, run the Oncreatview () method
So how do I make multiple fragment switch to each other without re-instantiating?
The correct way to switch is add (), toggle when Hide (), add () Another fragment, switch again, just hide () The current, show () another.
Previously shown fragment
Private Fragment mcontent;
/** modifying the displayed content does not reload **/
public void Switchcontent (Fragment to) {
if (mcontent! = To) {
Fragmenttransaction transaction = Getsupportfragmentmanager ()
. BeginTransaction ();
if (!to.isadded ()) {//First determine if the add
Transaction.hide (mcontent). Add (R.id.content, to). commit (); Hides the current fragment,add next to the activity
} else {
Transaction.hide (Mcontent). Show (To). commit (); Hides the current fragment, showing the next
}
Mcontent = to;
}
Showcontent ();
}

Android _ About fragment switch reload solution share to everyone

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.