Android Support Library Fragment Framework Java Library Library Technical Principles Exploration
Android Support Library Fragment Framework Java Library Library Technical Principles Exploration
introduction:
Android Support Library is a class library provided by Android to provide new feature support to the old version of Android devices.Among them, Fragment is an important component in SUPPORT LIBRARY, which provides a flexible mechanism to manage various components of the user interface.This article will explore the technical principles of the Android Support Library Fragment framework and provide relevant Java code examples.
1. Fragment Overview
Fragment is a reusable component in Android, which inherits the Android.app.fragment class.Fragment can be used as part of the Activity layout, dividing the interface into an independent module, making it easier for applications to maintain and expand.By using Fragment, you can manage multiple interfaces in the same Activity and implement modular development.
2. Fragment life cycle
Fragment's life cycle consists of a variety of states, including creation, operation, suspension, and destruction.It responds to these states by rewriting the relevant methods in Fragment.Common life cycle methods include:
1. Oncreate (): Used when the Fragment was created for initialization operations.
2. OncreateView (): It is called when the Fragment creation view view is used to load the layout file.
3. OnStart (): Calling when Fragment can be visible for some start -up related operations.
4. Onresume (): Fragment gets the focus when obtaining the focus to process the interactive logic of the interface.
5. Onpause (): Fragment is called when the focus is lost to save temporary data or stop some operations.
6. ONSTOP (): Fragment is not visible when it is not visible to release resources or stops time -consuming operations.
7. ONDESTROY (): Calling when Fragment was destroyed for cleaning work.
Third, application scenarios
Fragment can be used for a variety of scenarios, such as implementing multi -panel layout, creating a split screen interface, implementing TAB navigation, and processing horizontal and vertical screen switching.In SUPPORT LIBRARY, Fragment can provide compatibility with the old version of Android devices and simplify the development process.
Four, Fragment layout
Using SUPPORT LIBRARY FRAGMENT, you can create a Fragment layout through XML or code.Layout files can contain various UI elements, such as buttons, text boxes, images, etc. At the same time, other Fragment components can also be nested.The layout file can be dynamically modified through the View object to achieve the interactive effect of the interface.
The following is an example of creating a Fragment layout using XML:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
5. Fragment interaction
Through the SUPPORT LIBRARY FRAGMENT, the interaction between Fragment and Activity can be realized.You can define the interface in Fragment through the interface callback, and implement the interface in Activity, and call the interface method at appropriate time.This can realize the data to pass data or trigger some operations to Activity.
The following is an example of interaction about Fragment and Activity:
public class MainActivity extends AppCompatActivity implements MyFragment.OnFragmentInteractionListener {
@Override
public void onFragmentInteraction(Uri uri) {
// Process data passed by Fragment or the operation of trigger
}
}
public class MyFragment extends Fragment {
private OnFragmentInteractionListener mListener;
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
// Call mlistener.onfragmentInteraction (URI) method at the right time to inform Activity
}
6. Summary
Through the exploration of the technical principles of Android Support Library Fragment framework, we understand important contents such as Fragment's concept, life cycle, layout and interaction.Using SUPPORT LIBRARY FRAGMENT, we can better realize the modularization, layout flexibility and compatibility of the interface.I hope this article can help you understand the relevant knowledge of Fragment.