gpt4 book ai didi

java - 按返回时返回第一个 fragment

转载 作者:行者123 更新时间:2023-12-04 09:37:50 24 4
gpt4 key购买 nike

例如,当我在 Fragment 5 中按下返回按钮时,应用程序就会出现。我想要的是当我按下后退按钮时,我打开 fragment 1。我该怎么做?

我尝试了很多代码,但都不起作用,例如

FragmentManager fm = getActivity().getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}


getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

这是我在 MainActivity 中的 fragment 代码
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// make new object and find the view "BottomNavigationView"
BottomNavigationView navigationView = findViewById(R.id.bottom_navigation);
// To make the first fragment shows when the app start.
// We will ignore the "activity_main" and we will make new activity(fragment) called "fragment_home"
// and we will add all views we want to display them on it.
// (fragment_layout) it is a fragment have id was defined in "activity_main"
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout,
new HomeFragment()).commit();
// make Listener to call the fragments on buttons
// to start new fragment
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
// first fragment "home"
case R.id.home_item:
selectedFragment = new HomeFragment();
break;
// second fragment "videos"
case R.id.videos_item:
selectedFragment = new VideosFragment();
break;
// third fragment "ebadat"
case R.id.ebadat_item:
selectedFragment = new EbadatFragment();
break;
// fourth fragment "images"
case R.id.images_item:
selectedFragment = new ImagesFragment();
break;
// fifth fragment "more"
case R.id.settings_item:
selectedFragment = new MoreFragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, selectedFragment).commit();
return true;
}

});
}

最佳答案

首先,您必须在 Fragment 中实现 onBackPressed 方法。

  • 创建一个名为 IOnBackPressed 的接口(interface):
    点击 package -> new Java Class -> 并在 Kind 中选择 interface -> OK
    你的界面应该是这样的:
     public interface IOnBackPressed {
    boolean onBackPressed();
    }
  • 之后,您打开 MainActivity 并添加以下内容:
     @Override
    public void onBackPressed() {
    //super.onBackPressed(); delete this line
    // and start your fragment:

    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    if (!(fragment instanceof IOnBackPressed) || !((IOnBackPressed) fragment).onBackPressed()) {
    super.onBackPressed();
    }
    }
  • 你实现这个到你的 fragment :
     public class Profil_Fragment extends Fragment implements IOnBackPressed 
    并将其添加到 fragment 中:
      @Override
    public boolean onBackPressed() {
    //do what you want
    NextFragment nextFrag= new NextFragment();
    getActivity().getSupportFragmentManager().beginTransaction()
    .replace(R.id.fragment_container, nextFrag, "findThisFragment")
    .addToBackStack(null)
    .commit();

    return true;
    }
  • 关于java - 按返回时返回第一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62472105/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com