向片段添加返回按钮

时间:2018-09-07 07:09:05

标签: android android-fragments

我有一个底部导航栏,其中包含4个片段,当选择一个选项卡时,将加载该片段的新实例。

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
    = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment fragment = null;
    switch (item.getItemId()) {
        case R.id.navigation_home:
            fragment = HomeFragment.newInstance();
            break;
        case R.id.navigation_cards:
            fragment = CardsFragment.newInstance();
            break;
        case R.id.navigation_deals:
            fragment = DealsFragment.newInstance();
            break;
        case R.id.navigation_settings:
            fragment = SettingsFragment.newInstance();
            break;
    }
    if (fragment != null) {


        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content, fragment);
        fragmentTransaction.commit();
    }
    return true;
}
};

现在我的HomeFragment中有一个RecyclerView,在“项目”上选择它会打开一个新的片段:

   myViewHolder.cardView.setOnClickListener(view -> {
            System.out.println("clicked");

            Fragment fragment = new TargetDetailsFragment();
            FragmentTransaction ft = ((AppCompatActivity) context).getSupportFragmentManager()
                    .beginTransaction();
            ft.replace(R.id.content, fragment).addToBackStack(null);
            ft.commit();





        });

我想在TargetDetails Fragments中添加一个后退按钮,以便在选择后将您带回到首页,而我尝试通过在Main活动中实现OnBackStackChangedListener来做到这一点

@Override
public void onBackStackChanged() {
    shouldDisplayHomeUp();
}

public void shouldDisplayHomeUp(){
    //Enable Up button only  if there are entries in the back stack
    boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
    getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
}

@Override
public boolean onSupportNavigateUp() {
    //This method is called when the up button is pressed. Just the pop back stack.
    getSupportFragmentManager().popBackStack();
    return true;
}

}

但是问题是,当我单击它时,它会再次重新加载HomeFragment,但我只是希望它返回到该Fragment的已保存实例

4 个答案:

答案 0 :(得分:0)

通过在“活动”中添加以下代码。 片段返回栈可以被管理。

@Override
    public void onBackPressed() {

        int count = getFragmentManager().getBackStackEntryCount();

        if (count == 0) {
            super.onBackPressed();
            //additional code
        } else {
            getFragmentManager().popBackStack();
        }

    }

答案 1 :(得分:0)

你好,@ Bolu Okunaiya,我认为您应该尝试this,它将帮助您将后台管理为所需的片段,而无需再次加载相同的片段。

对于停止加载先前的片段,您应该在FragmentTransaction中使用“ add”而不是“ replace”

答案 2 :(得分:0)

这是我用于ViewPager的代码,其目的是查看当前页码是否为0,而不是继续super.onBackPressed();,否则转到上一个片段:

@Override
public void onBackPressed() {
    if(vpPager.getCurrentItem()!=0) {
        vpPager.setCurrentItem(vpPager.getCurrentItem()-1, true);
    } else {
        super.onBackPressed();
    }
}

答案 3 :(得分:0)

在您的MainActivity

@Override
        public void onBackStackChanged() {
            //shouldDisplayHomeUp();
            Fragment currentFragment = getActivity().getFragmentManager()
                              .findFragmentById(R.id.fragment_container);

          if (currentFragment instanceof TargetDetails) {
                  Log.v(TAG, "your current fragment is TargetDetails");
                  popBackStackImmediate(); //<<<< immediate parent fragment will open,which is your HomeFragement

                 }

        }

要使用popBackStackImmediate(),您需要*用FragmentB替换FragmentA,并在addToBackstack()之前使用commit()