fragment already exist in android

129 views Asked by At

**i've set here three onClickListener and tried to replace the fragment accordingly ... but whenever i try to click on the today onClickListener the application crashes and sends "fragment already exist" and points to fragment.setArguments(b) of today onClickListener....but works perfectly with other two onClickListener that are weekly and monthly...

**ANYONE PLEASE HELP **

today.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {


                Bundle b = new Bundle();
                b.putString("url", "");

                fragment.setArguments(b);

                fm.beginTransaction().replace(R.id.container, fragment).commit();
                fm.executePendingTransactions();
                today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
                monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
                weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));


            }
        });
        {

            weekly.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    fragment = new TodayFragment();
                    Bundle b = new Bundle();
                    b.putString("url", "");
                    fragment.setArguments(b);
                    fm.executePendingTransactions();
                    fm.beginTransaction().replace(R.id.container, fragment).commit();
                    weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
                    monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
                    today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));

                }
            });
        }

        monthly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragment = new TodayFragment();
                Bundle b = new Bundle();
                b.putString("url", "");
                fragment.setArguments(b);
                fm.executePendingTransactions();
                fm.beginTransaction().replace(R.id.container, fragment).commit();
                monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
                weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
                today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
            }
        });
1

There are 1 answers

0
nick9999 On

@Override

        public void onClick(View v) {


            Bundle b = new Bundle();
            b.putString("url", "");
            //Add this to create the new instance of the fragment 
            fragment = new TodayFragment();
            fragment.setArguments(b);

            fm.beginTransaction().replace(R.id.container, fragment).commit();
            fm.executePendingTransactions();
            today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
            monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
            weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));


        }
    });
    {

        weekly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragment = new TodayFragment();
                Bundle b = new Bundle();
                b.putString("url", "");
                fragment.setArguments(b);
                fm.executePendingTransactions();
                fm.beginTransaction().replace(R.id.container, fragment).commit();
                weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
                monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
                today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));

            }
        });
    }

    monthly.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fragment = new TodayFragment();
            Bundle b = new Bundle();
            b.putString("url", "");
            fragment.setArguments(b);
            fm.executePendingTransactions();
            fm.beginTransaction().replace(R.id.container, fragment).commit();
            monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
            weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
            today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
        }
    });

Create a Instance of Today Fragment in your code it will solve your problem.