Android Development Note (10)--Pass with fragment

Source: Internet
Author: User

Reprint Please specify: http://www.cnblogs.com/igoslly/p/6911165.html

Because recently sleepless in the development of the app, didn't have time to do a summary, not with very advanced parts, reluctantly also use some features to complete their first app, Sprinkle flower ~ ~ ~

followed by their own in the development of the use of some experience, but also with others to learn the results of practice, mainly about fragment.

/*add Fragment have static add & Add two ways * static is added <Fragment> control in layout layouts * because you can set the ID, you can find it by ID*/<Fragment android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/fragment1"/>/*Dynamic Add fragment is completely defined in the program add * add at the same time set tag, through the tag to find an instance (see Post transaction)*///management of the fragment in the activity requires the Fragment managerFragmentmanager Manager =Getfragmentmanager (); Manager.findfragmentbyid (); //Find by IDManager.findfragmentbytag ();//Search by TagManager.getfragments ();//get all the fragment that are add into the activity/*Manage current fragment, use Fragmenttransaction *transcation to control fragment display, add, replace, etc., such as Add/remove/replace *commit () The fragemnt of the operation is submitted to the system for display*/
replace (r.id.content_frame,fragment). commit (); // Replace--delete add operation 
Add (r.id.container,fragment1,string tag); // add fragment, and set dynamic find tag
Remove (fragment1);
// Delete
/*So fragment is often used to switch content in the navigation bar * The system calls the fragment layout and content of the response when the navigation bar is clicked on different icons, which is more convenient

========================================================================================================== * Activity or FRA Gment parameter passing to fragment
*/Bundle args=NewBundle (); Args.put ("Value_key", value);//Value_key is the keyword that is agreed in the transfer read//transfer values can be of various types, Int,string,array, etc.Fragment Fragment =NewFragment (); fragment.setarguments (args); // transfer bundles to fragment //Fragment Read ValueRed = Getarguments (). GetInt ("Value_key1"); yellow= Getarguments (). getString ("Value_key2");//======================================================================================================= ===
//When you need to return to the original activity from fragment, you need to set up the fragment connectionAheaddialogue.settargetfragment (competitionfragment. This, Request_code); Getfragmentmanager (). BeginTransaction (). replace (R.id.content_frame, fragment1). Addtobackstack (NULL). commit (); Getfragmentmanager (). Popbackstack (); //return//The original fragment receives the return value, which is defined in Onactivityresult//Request_code is a user-defined integer//ResultCode is usually the activity condition, whether the normal end Public voidOnactivityresult (intRequestcode,intResultCode, Intent data) {//Super.onactivityresult (Requestcode, ResultCode, data);//can judge different Requestcode accept different return value operation if(requestcode==0x1001) { if(ResultCode! = ACTIVITY.RESULT_OK) {//We 'll judge the status of the activity here . return; } Else{Hitplayer= Data.getstringextra ("Hitplayer"); Hitnumber= Data.getintextra ("Hitnumber", 0); } } if(requestcode==0x1111) { //Blablabla } }

Recommended introduction Fragment very detailed blog:http://blog.csdn.net/harvic880925/article/details/44927375

The contents of this note are personal learning, reproduced please specify the blog Park-igoslly

Android Development Note (10)--Pass with 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.