如何使用事务后台弹出碎片?

How can I use the transaction backstack to pop fragments?

假设我有一个带有 FragmentContainerView 的 MainActivity。 此 FragmentContainerView 的大小与 MainActivity 匹配。

然后我想在此 FragmentContainerView 中一次显示一个片段。

片段 1、片段 2 和片段 3。

每个片段都有一个按钮。按下按钮时,将加载下一个片段。

Fragment1(按下按钮)--> Fragment2(按下按钮)--> Fragment3(按下按钮)--> Fragment1 --> 依此类推。

到目前为止,我能够实现,但我不确定如何制作我的片段,当我按下后退按钮时,

Fragment1 应该退出应用程序

Fragment2 应该加载 Fragment1

Fragment3 应该加载 Fragment2

这是我的 MainActivity

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirstFragment fragment = new FirstFragment();
        Bundle bundle = new Bundle();
        bundle.putString("CLASS_NAME", " ");
        fragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container_view, fragment, null)
                .commit();
    }
}

这是我的 Fragment1

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

       
        binding = FragmentFirstBinding.bind(getView());


        String previous_class = getArguments().getString("CLASS_NAME");
        if(!previous_class.equals(" ")){
            binding.textViewFirstFragment.setText(previous_class);
        }

        //On button click, navigate to MainActivity
        binding.buttonFirstFragment.setOnClickListener(this);
    }

@Override
    public void onClick(View v) {
        //Log.i("BACKSTACK", "debug: current status of fragment back stack " + getFragmentManager().getBackStackEntryCount());
        Fragment fragment = new SecondFragment();
        putBundleArgument(fragment);
        replaceFragment(fragment);
        debug();
    }

    public void replaceFragment(Fragment fragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container_view, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

这是我的 Fragment2

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

   
    binding = FragmentSecondBinding.bind(getView());

    String previous_class = getArguments().getString("CLASS_NAME");
    binding.textViewSecondFragment.setText(previous_class);
    //On button click, navigate to MainActivity
    binding.buttonSecondFragment.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    //Log.i("BACKSTACK", "debug: current status of fragment back stack " + getFragmentManager().getBackStackEntryCount());
    Fragment fragment = new ThirdFragment();
    putBundleArgument(fragment);
    replaceFragment(fragment);
    debug();
}

public void replaceFragment(Fragment fragment) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container_view, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

最后,这是我的 Fragment3

 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    
    binding = FragmentThirdBinding.bind(getView());

    String previous_class = getArguments().getString("CLASS_NAME");
    binding.textViewThirdFragment.setText(previous_class);
    //On button click, navigate to MainActivity
    binding.buttonThirdFragment.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    Fragment fragment = new FirstFragment();
    putBundleArgument(fragment);
    replaceFragment(fragment);
    debug();

}
public void replaceFragment(Fragment fragment) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    /*
    ThirdFragment fragment3 = new ThirdFragment();
    transaction.remove(fragment3);
    SecondFragment fragment2 = new SecondFragment();
    transaction.remove(fragment2);
    */
    getFragmentManager().popBackStack();
    getFragmentManager().popBackStack();
    getFragmentManager().popBackStack();


    transaction.replace(R.id.fragment_container_view, fragment);
    transaction.addToBackStack(null);
    transaction.commit();

}
 @Override // override this in MainActivity
 public void onBackPressed(){
     int count = getSupportFragmentManager().getBackStackEntryCount();
     if(count == 1)//When there is only 1 fragment left in the stack ie Fragment 1.
        finsh();
     else
        getSupportFragmentManager().popBackStack(); //Fragment 2 and 3 are popped here.
     super.onBackPressed();  
 }