简体   繁体   中英

how to replace fragment from activity spinner without backstack

i have one activity which contains spinner control in appbar. in homeActivity i'm using fragment which have default fragment as parentoptionfragment, from that fragment there are 3 option to change the fragments, if i have choosen one fragment from parentoption fragment and i want to change spinner value then the fragment should be updated without adding to backstack means if i press back button then it should call parent optionfragment, but when i'm trying to do so i'm getting error.

    public void GetChildData(String token) {
    ParentOptionsFragment fragment =new ParentOptionsFragment();
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
    }

@Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            tv_childClassname.setText(classNameArr[position]);
            tv_childSchoolName.setText(schoolNameArr[position]);

            Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
            if (f instanceof ChildMapFragment){
                Toast.makeText(HomeActivity.this, "refreshing childmapfragment", Toast.LENGTH_SHORT).show();

                ChildMapFragment fragment = new ChildMapFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.popBackStackImmediate (fragment.getClass().getName(), 0);
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            }else if(f instanceof ParentOptionsFragment){
                Toast.makeText(HomeActivity.this, "spinner changed from ParentOptionsFragment", Toast.LENGTH_SHORT).show();

            }
        }

parentoptionfragment.java

public class ParentOptionsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_parent_options, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final ImageView img1=(ImageView)view.findViewById(R.id.imageView);
        ImageView img2=(ImageView)view.findViewById(R.id.imageView2);
        ImageView img3=(ImageView)view.findViewById(R.id.img_transport);

        img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChildMapFragment fragment = new ChildMapFragment();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        /*final ViewTreeObserver vto = img1.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int x;
                img1.getViewTreeObserver().removeOnPreDrawListener(this);
                x = img1.getMeasuredWidth();
                img1.setLayoutParams(new LinearLayout.LayoutParams(x,x));
                return true;
            }
        });*/
    }
}

childfragment.java

public class ChildFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_child_map, container, false);
    }
}

homeActivity ---> defaultfragment-- parentoptionfragment parentoptiofragment---> childfragment using spinner update childfragment without backstack onbackpress of childfragment --> parentoptionfragment

replace

Toast.makeText(HomeActivity.this, "refreshing childmapfragment", Toast.LENGTH_SHORT).show();

            ChildMapFragment fragment = new ChildMapFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.popBackStackImmediate (fragment.getClass().getName(), 0);
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();

with :

FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.popBackStack()

update : on your child fragment add a method : update() and call it :

FragmentManager fragmentManager = getSupportFragmentManager();
           Fragment  fragment = fragmentManager.findFragmentById(R.id.fragment_container)
    if(fragment isInstanceOf ChildMapFragment ){
    ((ChildMapFragment )fragment).update() //call your update method here
    }
FragmentManager fragmentManager = getSupportFragmentManager();
        //    String f_name=fragment.getClass().getName();
        if (!fragmentManager.popBackStackImmediate(tag, 0)) {

            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.replace(R.id.content_frame, fragment, tag);
            ft.addToBackStack(tag);
            ft.commit();

        }

to Refresh fragment implement back stack in activity like this

   @Override
    public void onBackStackChanged() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fr = fragmentManager.findFragmentById(R.id.content_frame);
        if (fr != null) {
            fr.onResume();
        }
    }

here pass different tag base on different fragment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM