Fragment popular is a fragment, can not exist alone, meaning must be attached to activity, generally there are two ways to add fragment to activity, divided into static, dynamic.
Static is right-click, set up a fragment, select Blank, in the activity layout directly add Fragment,name property directly to the previously established fragment, which adds fragment, this is relatively simple.
Dynamic:
What we're going to do is add a fragment,fragment in the activity with two buttons, switch different fragment when the button is pressed, and notice that switching between different fragment codes is written in the activity.
To achieve communication between fragment and activity, the steps are generally divided into five step, 1, declare an interface in fragment. 2. Implement the interface declared in fragment in activity.
3. Declare an interface object in fragment. 4, in the fragment life cycle of the Onattach method to determine whether the current activity implements the interface declared in this fragment
5. Call the method implemented in activity (interface object. Method Name)
First, create a fragment named Onefragment, tick the include fragment factory methods and include interface callback when right-click Build, which automatically adds the relevant code, Save a lot of time. Then add two buttons to the newly created fragment to set the ID.
The layout code is as follows
<framelayout 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"
tools:context= "Com.example.administrator.homeworepar.Fragment.OneFragment" >
<!--todo:update blank fragment layout --
<button
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:id= "@+id/bt1"
android:text= "Friends"/>
<button
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:id= "@+id/bt2"
android:text= "Message"
android:layout_gravity= "Center_horizontal|top"/>
</FrameLayout>
The following is the code for fragment
Package com.example.administrator.homeworepar.Fragment;
Import Android.content.Context;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import COM.EXAMPLE.ADMINISTRATOR.HOMEWOREPAR.R;
/**
* A Simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link Onefragment.onfragmentinteractionlistener} interface
* To handle interaction events.
* Use the {@link Onefragment#newinstance} factory method to
* Create an instance of this fragment.
*/
public class Onefragment extends Fragment {
Todo:rename parameter arguments, choose names that match
The fragment initialization parameters, e.g. Arg_item_number
private static final String arg_param1 = "param1";
private static final String arg_param2 = "param2";
Todo:rename and change types of parameters
Private String mParam1;
Private String mParam2;
Private Onfragmentinteractionlistener Mlistener;
Public Onefragment () {
Required empty Public constructor
}
/**
* Use this factory method to create a new instance of
* This fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Onefragment.
*/
Todo:rename and change types and number of parameters
public static Onefragment newinstance (String param1, string param2) {
Onefragment fragment = new Onefragment ();
Bundle args = new bundle ();
Args.putstring (arg_param1, param1);
Args.putstring (ARG_PARAM2, param2);
Fragment.setarguments (args);
return fragment;
}
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
if (getarguments () = null) {
mParam1 = Getarguments (). getString (ARG_PARAM1);
MPARAM2 = Getarguments (). getString (ARG_PARAM2);
}
}
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
Inflate the layout for this fragment
View view=inflater.inflate (R.layout.fragment_one, container, false);
button bt1= (button) View.findviewbyid (R.ID.BT1); The function of implementation Yes, the Changfragment method in the activity is called when the button is pressed
Bt1.setonclicklistener (New View.onclicklistener () {
@Override
Public void OnClick (View v) {
mlistener.changfragment (1);
}
});
button bt2= (button) View.findviewbyid (R.ID.BT2);
Bt2.setonclicklistener (New View.onclicklistener () {
@Override
Public void OnClick (View v) {
mlistener.changfragment (2);
}
});
return view;
}
@Override
Public void Onattach (context context) {
Super.onattach (context);
if (context instanceof Onfragmentinteractionlistener) {Determines if the interface is implemented, if the Switch the activity to an interface. So that the caller can Method
Mlistener = (onfragmentinteractionlistener) context;
} else {
throw new RuntimeException (context.tostring ()
+ "must implement Onfragmentinteractionlistener");
}
}
@Override
public void Ondetach () {
Super.ondetach ();
Mlistener = null;
}
/**
* This interface must is implemented by activities it contain this
* Fragment to allow a interaction in this fragment to be communicated
* To the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "Http://developer.android.com/training/basics/fragments/communicating.html"
* >communicating with other fragments</a> for more information.
*/
Public interface Onfragmentinteractionlistener {//This segment is an automatically added interface, there will be other code errors after implementing one method in the interface, as long as Delete the error code.
//todo:update argument type and name
void changfragment (int which);
}
And then create an activity,
Package com.example.administrator.homeworepar.Fragment;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import android.support.v7.app.AppCompatActivity;
Import COM.EXAMPLE.ADMINISTRATOR.HOMEWOREPAR.R;
public class Fragment2activity extends Appcompatactivity implements Onefragment.onfragmentinteractionlistener {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_fragment2);
}
@Override
Public void changfragment (int which) {
if (which==1) {
Fragment fragment1=new blankfragment (); Implement the method in fragment, when the resulting value is 1 o'clock, the blankfragment is filled//to the ID of FL2in Framelayout,
Getsupportfragmentmanager ()
. BeginTransaction (). replace (R.ID.FL2,FRAGMENT1). commit ();
}else if (which==2) {
Fragment fragment2=new secondfragment ();
Getsupportfragmentmanager ()
. BeginTransaction (). replace (R.id.fl2,fragment2). commit ();
}
}
}
The activity layout code is as follows
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Com.example.administrator.homeworepar.Fragment.Fragment2Activity" >
<fragment
android:layout_width= "Match_parent"
android:layout_height= "50DP"
android:name= "Com.example.administrator.homeworepar.Fragment.OneFragment"
android:id= "@+id/fragment3"/>
<framelayout
android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:id= "@+id/fl2"
android:layout_below= "@+id/fragment3" >
</FrameLayout>
</RelativeLayout>
This enables communication between the activity and the fragment, as well as a life-cycle problem with the same fragment as the activity.
Method Name Description
This function is called when Onattach () fragment is attached to the activity, and the host activity can be obtained in this method.
This function is called when onCreate () fragment is created.
This function is called when the layout of the Oncreateview () fragment is loaded.
Onactivitycreated () Call this function when the host activity has finished starting.
This function is called when OnStart () starts fragment.
This function is called when the Onresume () fragment is restored.
This function is called when the OnPause () fragment is paused.
This function is called when the OnStop () fragment is stopped.
This function is called when Ondestroyview () destroys the view control in fragment.
This function is called when OnDestroy () destroys fragment.
Ondetach () Fragment call this function when it is detached from activity
Communication between fragment's life cycle and activity, and the use