Demo代码
布局文件:activity_main.xml
<?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">
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#55ff0000"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnA"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="FragmentA"
/>
<Button
android:id="@+id/btnB"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="FragmentB"
/>
<Button
android:id="@+id/btnC"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="FragmentC"
/>
</LinearLayout>
</LinearLayout>
Fragment布局文件:FragmentA.java
package com.test.fragmentdemo;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class FragmentA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("我是FragmentA");
textView.setTextSize(30);
textView.setTextColor(Color.parseColor("#ff0000"));
return textView;
}
}
Fragment布局文件:FragmentB.java
package com.test.fragmentdemo;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class FragmentB extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("我是FragmentB");
textView.setTextSize(30);
textView.setTextColor(Color.parseColor("#00ff00"));
return textView;
}
}
Fragment布局文件:FragmentC.java
package com.test.fragmentdemo;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class FragmentC extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("我是FragmentC");
textView.setTextSize(30);
textView.setTextColor(Color.parseColor("#0000ff"));
return textView;
}
}
主界面:MainActivity.java
package com.test.fragmentdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private boolean isFirstClickB = true;
private boolean isFirstClickC = true;
private FragmentA fragmentA;
private FragmentB fragmentB;
private FragmentC fragmentC;
private List<Fragment> fragmentList = new ArrayList<Fragment>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentA = new FragmentA(); //new FragmentA()不会执行Fragment的生命周期
fragmentB = new FragmentB();
fragmentC = new FragmentC();
findViewById(R.id.btnA).setOnClickListener(this);
findViewById(R.id.btnB).setOnClickListener(this);
findViewById(R.id.btnC).setOnClickListener(this);
// 1.获取FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// 2.通过FragmentManager获取事务管理器
FragmentTransaction transaction = fragmentManager.beginTransaction();
// 3.将Fragment添加到帧布局中,初始显示为FragmentA
// 一般不这么用,这样操作加载会比较慢
/*transaction.add(R.id.fl, fragmentA, "FragmentA")
.add(R.id.fl, fragmentB, "FragmentB")
.add(R.id.fl, fragmentC, "FragmentC")
.hide(fragmentB)
.hide(fragmentC)
.commit();*/
transaction.add(R.id.fl, fragmentA, "FragmentA")
.commit();
addToBackStack(fragmentA);
}
private void addToBackStack(Fragment fragment) {
if (fragmentList.contains(fragment)) {
/**
* 先将集合列表中原有fragmet的删掉,再将fragment添加进集合列表的最后
* 这样操作是为了更改fragment在集合列表中的顺序
*/
fragmentList.remove(fragment);
fragmentList.add(fragment);
} else {
fragmentList.add(fragment);
}
}
@Override
public void onClick(View v) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.hide(fragmentA).hide(fragmentB).hide(fragmentC);
if (!isFirstClickB) {
transaction.hide(fragmentB);
}
if (!isFirstClickC) {
transaction.hide(fragmentC);
}
switch (v.getId()) {
case R.id.btnA:
transaction.show(fragmentA);
addToBackStack(fragmentA);
break;
case R.id.btnB:
if (isFirstClickB) {
transaction.add(R.id.fl, fragmentB, "FragmentB");
isFirstClickB = false;
}
transaction.show(fragmentB);
addToBackStack(fragmentB);
break;
case R.id.btnC:
if (isFirstClickC) {
transaction.add(R.id.fl, fragmentC, "FragmentC");
isFirstClickC = false;
}
transaction.show(fragmentC);
addToBackStack(fragmentC);
break;
default:
break;
}
//把当前事务添加到回退栈中, 但是该功能不太好用
//transaction.addToBackStack(null);
transaction.commit();
}
@Override
public void onBackPressed() {
//super.onBackPressed();
if (fragmentList.size() > 1) {
fragmentList.remove(fragmentList.size() - 1);
// 显示列表中最后一个Framgent布局
showFragment(fragmentList.get(fragmentList.size() - 1));
} else {
// 如果当前栈中没有Fragment或者只有一个Fragment,直接退出Activity
finish();
}
}
private void showFragment(Fragment fragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.hide(fragmentA).hide(fragmentB).hide(fragmentC);
transaction.show(fragment).commit();
}
}