Darko Petkovski
Darko Petkovski

Reputation: 3912

Fragment replace doesnt work

I'm having a radio group that changes my fragments in the activity when a the selected radiobutton is changed. I have this code:

@Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_menu1:
                showOffersFragment();
                break;
            case R.id.main_menu2:
                openMap();
                break;
            case R.id.main_menu3:
                showProductsFragment();
                break;
        }
    }

    private void showProductsFragment() {
        fm = getFragmentManager();
        ft = fm.beginTransaction();
        if (productsFragment == null) {
            bundle = new Bundle();
            bundle.putSerializable(ActivityConstants.PRODUCT_ITEMS, products);
            productsFragment = new ProductFragment();
            productsFragment.setArguments(bundle); ft.add(R.id.main_frag_container, productsFragment, "products");

        } else {
            //TODO
            ft.replace(R.id.main_frag_container, productsFragment, "products");
            productsFragment.loadList(products);
        }

        ft.commit();
    }
private void showOffersFragment() {
        Log.d(TAG, "OFFERS NULL OR :" + (offersFragment == null)+" ITEMS ="+items.size());

        fm = getFragmentManager();
        ft = fm.beginTransaction();

        if (offersFragment == null) {
            bundle = new Bundle();
            bundle.putSerializable(ActivityConstants.OFFER_ITEMS, items);
            offersFragment = new OffersFragment();
            offersFragment.setArguments(bundle);
            ft.add(R.id.main_frag_container, offersFragment,"offers");
        } else {
            ft.replace(R.id.main_frag_container, offersFragment,"offers");
        }
        ft.commit();
    }
  private void openMap() {
        fm = getFragmentManager();
        ft = fm.beginTransaction();
        if(mapFragment==null){
            mapFragment = new MapFragment();
            ft.add(R.id.main_frag_container, mapFragment, "Map");
        }else{
            ft.replace(R.id.main_frag_container, mapFragment, "Map");
        }
        ft.commit();
        mapFragment.getMapAsync(this);
    }

Now the problem is when I switch between fragments. Its not changing the fagments right away so its not responding very well. I should switch between them couple of times so the fragment can appear.

Can anyone tell me how can I optimize/make it work propperly?

Upvotes: 0

Views: 97

Answers (1)

JuliusScript
JuliusScript

Reputation: 3850

It does not respond right away, because at a start, once you choose each option, this line is run ft.add(R.id.main_frag_container, mapFragment, "Map");, which means you are adding new fragment into the same container instead of replacing an old one.

Use ft.replace(R.id.main_frag_container, mapFragment, "Map"); instead of ft.add(R.id.main_frag_container, mapFragment, "Map");. Though I might be wrong, but container (here R.id.main_fragment_container) has to have fragment already added, one option would be to add fragment only if container is empty, replace otherwise.

You might want to check addToBackStack if you wish to navigate back through previous fragments.

Upvotes: 1

Related Questions