Android Fragment is loaded into an Activity, androidfragment

Source: Internet
Author: User

Android Fragment is loaded into an Activity, androidfragment

Fragments is added after android3.0. It is mainly used to facilitate the development of android tablets. Easy to adapt to different sizes of screens. This code is used to add Fragment to an Activity for the simplest use of Fragment. The main knowledge points involved are: 1. Creation of the Fragment class, 2. Add Fragment. 3. Add a Fragment without a UI. Retrieve the Fragment based on the Tag.

The Xml layout file corresponding to Fragment, <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "android: weightSum =" 10 "> <Button android: id =" @ + id/bt "android: layout_width =" match_parent "android: layout_height =" 0dp "android: t Ext = "add a Fragment" android: layout_weight = "2"/> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "loads Fragment into the Activity. This Fragment does not have a UI, that is, the onCreateView method is not required, it can be used as the background color of this Activity "/> <Button android: id =" @ + id/button2 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "find Fragment based on the Fragment Tag"/> <L InearLayout android: id = "@ + id/lv_fragment_container" android: layout_width = "match_parent" android: layout_height = "0dp" android: layout_weight = "9" android: background = "#123456" android: orientation = "horizontal"> </LinearLayout> 1. Create package com. example. fragment1; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import Android. view. viewGroup;/*** @ author sea * to create a Fragment, you must implement at least three life cycle functions onCreate, onCreateView, onPause **/public class MyFragment extends Fragment {/** initialize Fragment and instantiate the member variable in Fragment */@ Override public void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState);}/** load the UI layout for Fragment, and return the Stuff corresponding to the Fragment layout file */@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// TODO Auto-generated method stub View view = inflater. inflate (R. layout. fragment, container, false); return view;}/** call */@ Override public void onPause () when the user leaves this Fragment () {// TODO Auto-generated method stub super. onPause () ;}} 2. Create package com. example. fragment1; import android. app. fragment; import android. o S. bundle; public class MyFragment2 extends Fragment {@ Override public void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState);} @ Override public void onPause () {// TODO Auto-generated method stub super. onPause () ;}} 3. Add Fragment to Activity package com. example. fragment1; import android. OS. bundle; import android. app. activity; import android. a Pp. fragmentManager; import android. app. fragmentTransaction; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button;/*** @ author sea * load the fragment to an Activity * method 1 code: In this example, FragmentTransaction class * is used. Method 2: add **/public class MainActivity extends Activity {private Button button; @ Override protected void onCreate (Bundle savedIns TanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. bt); button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// find FragmentTransaction FragmentManager fragmentManager = getFragmentManager (); FragmentTransaction fragmentTransaction = fragmentManager. beginTransaction (); MyFragment fragme Nt = new MyFragment (); // Add it to the Activity fragmentTransaction. add (R. id. lv_fragment_container, fragment); // Add it to the background stack. If you have the next code, click the return button to return to the Activity page. If not, directly exit Activity // The following parameter is the Tag of this Fragment. Equivalent to id fragmentTransaction. addToBackStack ("fragment1"); // remember to submit fragmentTransaction. commit ();}});}}
How does android development jump from fragment to another activity through controls?

Intent in = new intent (getactivity (), activity to jump );

The fragment of the new Android feature, in which how to jump to other unrelated activities through the button

GetActivity () can be used in fragment.

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.