返回时返回第一个片段

时间:2020-06-19 14:10:03

标签: java android

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

我尝试了很多代码,但无法正常工作,例如

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

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

这是我在MainActivity中的片段代码

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;
        }

    });
}

4 个答案:

答案 0 :(得分:1)

添加onBackPressed方法:

@Override
public void onBackPressed() {
    //super.onBackPressed(); delete this line 
    // and start your fragment:

    NextFragment nextFrag= new NextFragment();
    getActivity().getSupportFragmentManager().beginTransaction()
         .replace(R.id.Layout_container, nextFrag, "findThisFragment")
         .addToBackStack(null)
         .commit();
}

答案 1 :(得分:1)

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

  1. 创建一个名称为IOnBackPressed的接口: 单击包->新Java类->并在种类中选择接口->确定 您的界面应如下所示:

     public interface IOnBackPressed {
     boolean onBackPressed();
     }
    
  2. 之后,打开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();
         }
     }
    
  3. 您将其实现为片段:

     public class Profil_Fragment extends Fragment implements IOnBackPressed 
    

    并将其添加到片段中:

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

答案 2 :(得分:0)

添加backstack和标签(您可以使用“ HomeFragment”之类的片段名称)来替换片段。

replace(fragmentId, fragment, fragmentName).addToBackStack(fragmentName)

还有这样的弹出片段

popBackStack(fragmentName,0)

答案 3 :(得分:0)

尝试添加transaction.addToBackStack(tag);

例如:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_layout, selectedFragment);
ft.addToBackStack( "tag" ).commit();

这会将您的片段添加到后退堆栈,以便后退按钮可以正常工作。