繁体   English   中英

如何使用事务回栈来弹出片段?

[英]How can I use the transaction backstack to pop fragments?

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

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

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

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

Fragment1 (Press button)--> Fragment2 (Press button)--> Fragment3 (Press button)--> 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();
    }
}

这是我的片段1

    @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();
    }

这是我的片段2

@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();  
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM