On the transaction operation of fragment in the development of Android app _android

Source: Internet
Author: User

In Android, fragment operations are performed through Fragmenttransaction. From the result of fragment, the operation of fragment in Fragmenttransaction can be divided into two types:

Displayed: Add () replace () show () Attach ()
Hide: Remove () Hide () detach ()
For each set of methods, although the result is similar, the side effects of the method and the impact on the life cycle of the fragment are different.

Add () vs. replace ()
the difference between calling add () or replace () can only be reflected if the number of fragment is greater than or equal to 2. The Onattach ()-onresume () of each fragment lifecycle is called once, when the fragment is added twice in succession through Add (). And the view of the two fragment will be attach to Containerview at the same time.

Similarly, when you exit Activty, the OnPause ()-ondetach () of each fragment lifecycle is also called once.

But when the replace () is used to add fragment, the second addition causes the first fragment to be destroyed, i.e. the first fragment () is executed before the Onattach () method of the second fragment is executed- Ondetach () method, while Containerview will detach the first fragment view.

Show () & Hide () vs. attach () & Detach ()
when the show () & Hide () method is invoked, the fragment lifecycle method is not executed, only fragment view is displayed or hidden. Moreover, although fragment's view is hidden, it is not detach in the parent layout and remains as a containerview childview. In contrast, attach () & Detach () is more thorough. Once a fragment is detach (), its onpause ()-ondestroyview () cycle is executed.

At the same time fragment's view will also be detach. After the attach () is called again, the Oncreateview ()-onresume () cycle is executed again.

Remove ()
actually read the above analysis, remove () method basic also understand. Relative to the Add () method executes the lifecycle of the Onattach ()-onresume (), remove () is the remaining onpause ()-ondetach () cycle.

Error

The following errors have been encountered when writing fragmenttransaction:

Copy Code code as follows:

The method replaces (int, Fragment) in the type fragmenttransaction isn't applicable for the arguments (int, myfragment)

Fragment newfragment =new myfragment ();
Fragmenttransaction.replace (r.layout.activity_main,newfragment). commit ();

Hint Error: The method replaces (int, Fragment) in the type fragmenttransaction isn't applicable for the arguments (int, myfragmen T

Reason to find a long time! The subclass object has been assumed to be unable to be assigned to a parent class reference. It's not science!

The code is like this:

 package com.example.testforfragment;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.FragmentManager;
Import android.app.FragmentTransaction;
Import android.support.v4.app.Fragment;
Import android.support.v4.app.fragment.*;

Import Android.view.Menu; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super.onc
  Reate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
  Fragmentmanager Fragmentmanager = Getfragmentmanager ();
  Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
  Fragment newfragment =new myfragment ();
 Fragmenttransaction.replace (r.layout.activity_main,newfragment). commit ();  @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action Bar if
  It is present.
  Getmenuinflater (). Inflate (R.menu.main, menu);
 return true; }

}

So the key is:

Fragment newfragment =new myfragment ();
Fragmenttransaction.replace (r.layout.activity_main,newfragment). commit ();

After modification:

Package com.example.testforfragment;
Import Android.os.Bundle;
Import android.app.Activity;
Import android.support.v4.app.Fragment;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentManager;
Import android.support.v4.app.FragmentTransaction;

Import Android.view.Menu; public class Mainactivity extends fragmentactivity {@Override protected void onCreate (Bundle savedinstancestate) {s
  Uper.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
  Fragmentmanager Fragmentmanager = Getsupportfragmentmanager ();
  Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
  Fragment newfragment =new myfragment ();
 Fragmenttransaction.replace (r.layout.activity_main,newfragment). commit ();  @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action Bar if
  It is present.
  Getmenuinflater (). Inflate (R.menu.main, menu);
 return true; }

}

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.