Simple Android Fragment instance
The interface display on Android is implemented through Activity, which is too common. I believe everyone is familiar with it and I will not go into details here. However, Activity also has its limitations. The same interface may be nice to display on the mobile phone, but it may not be good on the tablet, because the screen of the tablet is very large, when the mobile phone interface is placed on the tablet, it may be overly stretched, and the control spacing is too large. In this case, the better experience is to embed "small Activity" in the Activity, and then each "small Activity" can have its own layout. This is the Fragment fragmentation technology.
1. Fragment Introduction
Android introduced Fragment in Android 3.0 (API level 11. You can think of Fragment as a module in the Activity. This module has its own layout, lifecycle, and processing of its own input. The Fragment module can be loaded or removed when the Activity is running. Fragment can be designed as a module that can be reused in multiple activities. Fragment can be used to achieve flexible layout and improve the user experience when developing applications for both tablet and mobile phones.
Ii. Fragment Lifecycle
Because Fragment must be embedded in Acitivity, the lifecycle of Fragment is closely related to its Activity.
If the Activity is paused, all Fragment is paused. if the Activity is stopped, all Fragment in the Activity cannot be started. If the Activity is destroyed, all Fragment in it will be destroyed. However, when the Activity is active, you can independently control the Fragment status, such as adding or removing Fragment.
When performing the fragment transaction (conversion), you can put the fragment in the back stack of the Activity so that the user can perform the return operation.
Comparison between Activity and Fragment Lifecycle
3. Two simple examples: simple Fragment exercises, and Activity communication with Fragment
Layout file activity_main.xml
<framelayout android:id="@+id/content" android:layout_height="0dp" android:layout_weight="1" android:layout_width="wrap_content"> </framelayout>
Java files
package com.example.fragmenttabhost;import android.os.Bundle;import android.support.v4.app.ListFragment;import android.widget.ArrayAdapter;import android.widget.Toast;public class MyFragment extends ListFragment{ String show1[] = {"1.1","1.2","1.3","1.4"}; String show2[] = {"2.1","2.2","2.3","2.4"}; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String show[] = null; Bundle bundle = getArguments(); if(bundle == null) show = show1; else { show = show2; Toast.makeText(getActivity(), (CharSequence) bundle.get("key"), 1).show(); } setListAdapter(new ArrayAdapter
(getActivity(), android.R.layout.simple_list_item_1, show)); }}
package com.example.fragmenttabhost;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;public class MainActivity extends FragmentActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentTabHost tabHost = (FragmentTabHost) findViewById(R.id.tab); tabHost.setup(this, getSupportFragmentManager(), R.id.content); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1"), MyFragment.class, null); Bundle b = new Bundle(); b.putString("key", "I am tab2"); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2",getResources().getDrawable(R.drawable.ic_launcher)), MyFragment.class, b); }}
Running result
Vc + 6w7 + 0tcRkZW1vo6zEo7fCzqLQxdb30rPD5jxiciAvPg0KPGltZyBhbHQ9 "here write picture description" src = "http://www.bkjia.com/uploads/allimg/150606/0424324096-5.png" title = "\"/>
Fragment_1, 2, 3, 4, 5.xml
Tab_item_view.xml button Layout
Main_tab_layout.xml
<framelayout android:id="@+id/realtabcontent" android:layout_height="0dip" android:layout_weight="1" android:layout_width="fill_parent"> <framelayout android:id="@android:id/tabcontent" android:layout_height="0dp" android:layout_weight="0" android:layout_width="0dp"> </framelayout></framelayout>
FragmentPage1, 2,3, 4, 5. java fragment implementation
package com.vhreal.myfragmenttest;import com.vhreal.myfragmenttest.R;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentPage1 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_1, null); } }
Implement MainTabActivity. java on the home page
Package com. vhreal. myfragmenttest; import com. vhreal. myfragmenttest. r; import android. OS. bundle; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentTabHost; import android. view. layoutInflater; import android. view. view; import android. widget. imageView; import android. widget. tabHost. tabSpec; import android. widget. textView;/*** @ author vhreal * Function Description: Custom TabHost */public class MainTabActivity extends FragmentActivity {// defines the FragmentTabHost object private FragmentTabHost mTabHost; // define a layout private LayoutInflater layoutInflater; // define an array to store the Fragment interface private Class fragmentArray [] = {FragmentPage1.class, FragmentPage2.class, FragmentPage3.class, comment, comment }; // define an array to store the button image private int mImageViewArray [] = {R. drawable. tab_home_btn, R. drawable. tab_message_btn, R. drawable. tab_selfinfo_btn, R. drawable. tab_square_btn, R. drawable. tab_more_btn}; // The text on the Tab is private String mTextviewArray [] = {"Homepage", "message", "friend", "square", "more "}; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main_tab_layout); initView ();}/*** initialization component */private void initView () {// instantiate the layout object layoutInflater = LayoutInflater. from (this); // instantiate the TabHost object and get TabHost mTabHost = (FragmentTabHost) findViewById (android. r. id. tabhost); mTabHost. setup (this, getSupportFragmentManager (), R. id. realtabcontent); // get the number of fragment int count = fragmentArray. length; for (int I = 0; I <count; I ++) {// set the icon, text, and content TabSpec tabSpec = mTabHost for each Tab button. newTabSpec (mTextviewArray [I]). setIndicator (getTabItemView (I); // Add the Tab button to the mTabHost Tab. addTab (tabSpec, fragmentArray [I], null); // set the background of the Tab button mTabHost. getTabWidget (). getChildAt (I ). setBackgroundResource (R. drawable. selector_tab_background) ;}}/*** set the icon and text for the Tab button */private View getTabItemView (int index) {View view = layoutInflater. inflate (R. layout. tab_item_view, null); ImageView imageView = (ImageView) view. findViewById (R. id. imageview); imageView. setImageResource (mImageViewArray [index]); TextView textView = (TextView) view. findViewById (R. id. textview); textView. setText (mTextviewArray [index]); return view ;}}
Running result
4. For details about android Fragment, refer to the introduction to Android Fragment. The basic introduction to Android Fragment is true full resolution (up and down ).