How to operate in a fragment or discretionary class there is a method in the fragment

Source: Internet
Author: User

1 How to run the Fragment method in Acitivty:

First get the object of this fragment

Xxxfragment Fragmentobject = (xxxfragment) getfragmentmanager.findfragmentbytag ("xx");

2 How to run the activity in fragment:

First: Let acitivity inherit the interface

Another: Use Getactivity () in fragment, but add Acitivity's name, namely:

((mainactivity) getactivity ()). XXX ();


3 assume that in fragment to operate a fragment, the first thing to get this object, how to get? Using the Getfragmentbytag of Fragmentmnager in Getactivity, you can then use this fragment object to manipulate his method.

Of course, when you get this fragment, turn it into an object of this fragment. Eg:

Fragmentmanager fm = Getactivity.getsupportfragmentmanager ();

Xxxfragment = (xxxfragment) fm.findfragmentbytag ("xxx")


4 How to operate a fragment in a random class, first of all to get the environmental parameters, how to get?

In the activity :

private static weakreference<actionbuttonactivity> actionbuttonactivty = null;
actionbuttonactivty = new Weakreference<actionbuttonactivity> (this);
This Actionbuttonactivity object is passed from activity to this random class.

Asynctask.setactivity (actionbuttonactivty);
InCasual ClassIn

private static weakreference<actionbuttonactivity> actionbuttonactivty;
public void setactivity (weakreference<actionbuttonactivity> actionbuttonactivty) {this.actionbuttonactivty = Actionbuttonactivty;}

/** * This method was invoked on the UI thread after the background computation * finishes. The result of the background computation is passed to this step * as a parameter. */@Overrideprotected void OnPostExecute (Bitmap result) {Super.onpostexecute (result); Fragmentmanager fm = Actionbuttonactivty.get (). Getfragmentmanager (); Fragmenttransaction ft = fm.begintransaction (); Bfragmenttab_one_event_details Bt_det = (bfragmenttab_one_event_details) fm.findfragmentbytag ("2_det"); Bt_ Det.setevidenceimage (result); bt_det.setbuttonclickable (); Ft.addtobackstack (null). commit ();}


OK The call relationship between all classes and fragment has been described above.




How to operate in a fragment or discretionary class there is a method in the fragment

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.