.replace没有按预期工作

时间:2017-10-26 12:58:02

标签: android android-fragments

我正在尝试一个底部导航视图,每个项目都有一个片段。我在Fragment之间改变的代码如下:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_host, container, false);

    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

    fm = getActivity().getSupportFragmentManager();
    final BottomNavigationView bottomNavigationView = (BottomNavigationView)
            view.findViewById(R.id.navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment;
            FragmentTransaction transaction = fm.beginTransaction();
            switch (item.getItemId()){
                case R.id.countries_bottom:
                    selectedFragment = fm.findFragmentByTag(COUNTRIES_TAG);
                    // If fragment doesn't exist yet, create one
                    if (selectedFragment == null) {
                        transaction.add(R.id.frame_layout, new CountriesFragment(), COUNTRIES_TAG);
                        Log.d("FRAGMENTS",  "COUNTRIES CREATED");
                    }
                    else { // re-use the old fragment
                        transaction.replace(R.id.frame_layout, selectedFragment, COUNTRIES_TAG);
                        Log.d("FRAGMENTS",  "COUNTRIES REPLACED");
                    }
                    break;
                case R.id.cities_bottom:
                    selectedFragment = fm.findFragmentByTag(CITIES_TAG);
                    // If fragment doesn't exist yet, create one
                    if (selectedFragment == null) {
                        transaction.add(R.id.frame_layout, new CitiesFragment(), CITIES_TAG);
                        Log.d("FRAGMENTS",  "CITIES CREATED");
                    }
                    else { // re-use the old fragment
                        transaction.replace(R.id.frame_layout, selectedFragment, CITIES_TAG);
                        Log.d("FRAGMENTS",  "CITIES REPLACED");
                    }
                    break;
                case R.id.places_bottom:
                    selectedFragment = fm.findFragmentByTag(PLACES_TAG);
                    // If fragment doesn't exist yet, create one
                    if (selectedFragment == null) {
                        transaction.add(R.id.frame_layout, new PlacesFragment(), PLACES_TAG);
                        Log.d("FRAGMENTS",  "PLACES CREATED");
                    }
                    else { // re-use the old fragment
                        transaction.replace(R.id.frame_layout, selectedFragment, PLACES_TAG);
                        Log.d("FRAGMENTS",  "PLACES REPLACED");
                    }
                    break;
            }
            transaction.commit();
            return true;
        }
    });

    return view;
}

当我这样做时,他们会跑,但有时候很奇怪。让我们称他们为1,2,3。当我在1,然后转到2时,日志显示“已创建”,因此创建了2,但后面仍然可以看到1。然后,当我回到1时,它会显示“REPLACED”,看起来很好。然后,我重新回到两个而不是说“REPLACED”它再次说“CREATED”,所以我不确定在这种情况下替换是如何工作的,我怎么能这样做我每个只能有1个实例片段

如果我从1变为2然后变为3,那么当我回到1时,1将不会被“替换”,而是“再次创建”。

编辑:所以搜索我了解到替换将从堆栈中完全删除我的片段,所以我决定不再替换,显示片段并隐藏其余部分(如果存在的话)。即使这确实解决了问题,而且应用程序基本上可以满足我的需求,但我确信这不是最好的解决方案。

以下是我做的快速更改的示例:

case R.id.countries_bottom:
                    selectedFragment = fm.findFragmentByTag(COUNTRIES_TAG);
                    // If fragment doesn't exist yet, create one
                    if (selectedFragment == null) {
                        transaction.add(R.id.frame_layout, new CountriesFragment(), COUNTRIES_TAG);
                        Log.d("FRAGMENTS",  "COUNTRIES CREATED");
                    }
                    else { // re-use the old fragment
                        transaction.show(selectedFragment);
                        Log.d("FRAGMENTS",  "COUNTRIES REPLACED");
                    }
                    if(fm.findFragmentByTag(CITIES_TAG) != null )
                        transaction.hide(fm.findFragmentByTag(CITIES_TAG));
                    if(fm.findFragmentByTag(PLACES_TAG) != null )
                        transaction.hide(fm.findFragmentByTag(PLACES_TAG));
                    transaction.commit();
                    break;

1 个答案:

答案 0 :(得分:0)

  

然后,我又回到了两个而不是说'#34;替换"它说'#34;创建"试。

我不是百分百肯定,但这是因为当你回到片段1时,片段2将从后台堆栈中移除,不再存在。

  如果我从1,到2再到3,那么当我回到1时,1将不会是#34;更换"而是"创建"试。

// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
                           .add(detailFragment, "detail")
                           // Add this transaction to the back stack
                           .addToBackStack()
                           .commit();

如您所见,有一个.addToBackStack()方法。我建议在添加片段时使用此方法。

此代码已从文档中删除: https://developer.android.com/training/implementing-navigation/temporal.html

希望我有所帮助。