Android 10-fragment, Android-fragment
Android 10-day notes-fragmentFragment (Fragment) I. Fragment Introduction
* Fragment is an API introduced in 3.0, mainly to solve the display problem of tablets and large screen mobile phones.
* Fragment represents the sub-module of the Activity. Therefore, fragment can be understood as a part of the Activity.
* Fragment must be used in an embedded Activity.
2. Step 3 of creating Fragment use fragment use Activity
* Create a layout to fill in fragment
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "sound size"/> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Speed"/> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "3D"/> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "left channel"/> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "right channel"/> </LinearLayout>
* Create a fragement public class SoundFragment extends Fragment {
@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.sound_item,null) ; return view;}
* Main Interface Layout
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layoutwidth = "matchparent" android: layoutheight = "matchparent" android: orientation = "vertical"> <! -- Container --> <FrameLayoutandroid: id = "@ + id/fl_container" android: layout_width = "match_parent" android: layout_height = "0dp" android: layout_weight = "3"> </FrameLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content"> <Button android: onClick = "sound" android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "wrap_content" android: text = "sound"/> </LinearLayout>
* Activity
/*** Click the sound button to display fragment * @ param v */public void sound (View v) {// declare Fragment SoundFragment soundFragment = new SoundFragment (); // get the Fragment manager FragmentManager = getFragmentManager (); // start the transaction FragmentTransaction transaction = manager. beginTransaction (); // Replace the transaction of the frame layout container in the current layout. replace (R. id. fl_container, soundFragment); // submit the transaction. commit ();}
4. Lifecycle
- OnAttach --- used to establish a connection and disconnect with Acivity. When this Fragment is added to the Activity, it is called only once.
- OnCreate --- called when creating Fragment
- OnCreateView -- it is called every time Fragment is created.
- OnActivityCreated: called when the Activity in which Fragment is located is started
- OnStart: called when Fragment is started
- OnResume: it is called back when the Fragment is restored and will be called after the onStart method.
- OnPause: callback when Fragment is suspended
- OnStop: callback when Fragment is stopped
- OnDestroyView: create and destroy a view
- Ondescloud: used for initialization and destruction, the same as in the Activity.
- OnDetach: Delete the Fragment from the Activity. Call this method when the replacement is complete. Call this method after the onDestroy method.
Comparison between Activity and Fragment Lifecycle
Activity--------------------Fragment onAttach() onCreate() onCreateView()onCreate() onActivityCreatedonStart() onStart()onResume() onResume()onPause() onPause()onStop() onStop() onDestoryView() onDestory()onDestory() onDetach()