1. Life cycle
- OnAttach () : Called when Fragment is associated with Activity. You can get Activity references through this method, and you can get parameters through getArguments().
- OnCreate () : called when the Fragment is created
- OnActivityCreated () : called when the Activity completes onCreate()
- OnStart () : called when the Fragment is visible.
- OnResume () : called when the Fragment is visible and interactive
- OnPause () : called when the Fragment is visible but not interactive.
- OnStop () : called when Fragment is not visible.
- OnDestroyView () : Called when the Fragment’s UI is removed from the view structure.
- OnDestroy () : called when the Fragment is destroyed.
- OnDetach () : called when the Fragment and Activity are unassociated.
2. Use
2.1 Static Loading
1. Define the Fragment layout and create left_fragment. XML and right_fragment. XML files
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
Copy the code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="this is Fragment" />
</LinearLayout>
Copy the code
- Create a custom Fragment class that inherits the Fragment or its subclass and overwrites onCreateView(), calling the inflater.inflate() method in the method to load the Fragment layout file and then returning the loaded view object
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container,false);
returnview; }}Copy the code
public class RigthFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container, false);
returnview; }}Copy the code
- Add the Fragment tag to the layout file corresponding to the Activity that needs to load the Fragment
<fragment
android:id="@+id/left_fragment"
android:name="com.test.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/right_fragment"
android:name="com.test.RigthFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
Copy the code
- Call setContentView() in the Activity’s onCreate() method to load the layout file
Note: Static loads once added cannot be removed at run time
2.2 Dynamic Loading
Chestnut:
- As with static loading, you define the Fragment’s layout and class first, modify the main layout file, and do not specify the tag’s name attribute.
- Implementing Fragment calls
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
replaceFragment(new RigthFragment());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
replaceFragment(new AnotherRightFragment());
break;
default:
break; }}private void replaceFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.commit(); }}Copy the code
3. Fragment Communicates with the Activity
3.1 An Activity sends data to a Fragment (using the Bundle mode)
chestnuts
- The layout file for the Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="I am the Activity" />
<FrameLayout
android:layout_below="@+id/button"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="500dp"/>
</LinearLayout>
Copy the code
- Set the Fragment layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
>
<TextView
android:id="@+id/fragment"
android:text="I am fragments"
android:layout_gravity="center"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Wait for the Activity to send a message" />
<Button
android:id="@+id/button"
android:layout_gravity="center"
android:text="Click to receive Activity message"
android:layout_centerInParent="true"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Copy the code
- Set the Activity’s class file
public class Activity2Fragment extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activcity_2_fragment);
text = (TextView) findViewById(R.id.text);
// Step 1: Obtain the FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// Step 2: Obtain FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Step 3: Create the Fragment to be added
final mFragment fragment = new mFragment();
// Step 4: Create the Bundle object
// Stores data and passes it to the Fragment
Bundle bundle = new Bundle();
// Step 5: Add data to the bundle
bundle.putString("message"."I love Google");
// Step 6: Set the data to the Fragment
fragment.setArguments(bundle);
// Step 7: Dynamically add fragments
// Add the fragment to the placeholder defined in the Activity layout file (FrameLayout)fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit(); }}Copy the code
- Set the Fragment class file
public class mFragment extends Fragment {
Button button;
TextView text;
Bundle bundle;
String message;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.fragment, container, false);
// Set the layout file
button = (Button) contentView.findViewById(R.id.button);
text = (TextView) contentView.findViewById(R.id.text);
// Step 1: Getaranswers () to get all the values passed in from the Activity
bundle = this.getArguments();
// Step 2: Get a value
message = bundle.getString("message");
// Step 3: Set the button to display the set value
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Display the value passedtext.setText(message); }});returncontentView; }}Copy the code
3.2 Fragment passing data to an Activity(interface callback)
- The layout file for the Activity
<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="scut.carson_ho.fragment_2_activity.MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Waiting for a Fragment to send a message" />
<Button
android:id="@+id/button"
android:layout_below="@+id/text"
android:text="Click to receive Fragment message"
android:layout_centerInParent="true"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_below="@+id/button"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="500dp"/>
</RelativeLayout>
Copy the code
- Set the Fragment layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/fragment"
android:text="I am fragments"
android:gravity="center"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
</LinearLayout>
Copy the code
- Set up the callback interface
public interface ICallBack {
void get_message_from_Fragment(String string);
}
Copy the code
- Set the Fragment class file
public class mFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.fragment, container, false);
// Set the layout file
return contentView;
}
// Set the interface callback method
public void sendMessage(ICallBack callBack){
callBack.get_message_from_Fragment("Message: I'm from Fragment"); }}Copy the code
- Set the Acticvity class file
public class MainActivity extends AppCompatActivity {
Button button;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
text = (TextView)findViewById(R.id.text);
// Step 1: Obtain the FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// Step 2: Obtain FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Step 3: Create the Fragment to be added
final mFragment fragment = new mFragment();
// Step 4: Dynamically add fragments
// Add the fragment to the placeholder defined in the Activity layout file (FrameLayout)
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Send a message from the fragment to the Activity via an interface callback
fragment.sendMessage(new ICallBack() {
@Override
public void get_message_from_Fragment(String string) { text.setText(string); }}); }}); }}Copy the code