简体   繁体   中英

how to replace the fragment1 from fragment2 from a button click of a dialog[NOT DIALOG FRAGMENT] (which is called by fragment1)

I am facing a problem where I am opening a dialog (Dialog 1) from fragment1 and there is a button (change) on Dialog1 on which if I click then: Dialog1 should be dismissed and on the same fragment1 should be replaced by fragment2(Another fragment)

    public class PedigreeAnalysis extends Fragment //My Fragment1 
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
                         showDialog();
        }

    // SHOW DIALOG FUNCTION IS SHOWN BELOW ` 
       
         void showDialog() { //Function to show the dialog starts...
        
            Dialog dialog= new Dialog(getActivity());
            Button btn = dialog.findViewById(R.id.button);
            btn.setOnClickListener(new View.OnClickListener() {
              @Override
            public void onClick(View view) {
            //Code for opening new fragment and dismissing the dialog.
                   getActivity().getSupportFragmentManager().beginTransaction()
                      .replace(R.id.fragment1, new Fragment2()).commit();

              dialog.dismiss();
                        }
                    });
        }//Function Ends Here....

I have even tried the reverse logic of (dismissing the dialog first and then replacing it with the function but it also doesn't works) as :

                 dialog.dismiss();//First dismissing the dialog
                 getActivity().getSupportFragmentManager().beginTransaction()
                      .replace(R.id.fragment1, new Fragment2()).commit();//Replacing the fragment

             

Follow this...

Fragment1:

public class Fragment1 extends Fragment {
    private ItemClickListener itemClickListener;
    private Button addButton;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // all the views are initialized here...
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        addButton.setOnClickListener(v -> {
            if (itemClickListener != null) onItemClicked.onClicked(new Plan());
        });
    }

    public Fragment1 setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
        return this;
    }
    public interface ItemClickListener {
        void onItemClicked(Plan plan);
    }
}

In parent Activity Class

public class PlanActivity extends AppCompatActivity {
    private Fragment1 fragment1;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan);
        
        fragment1 = new Fragment1()
                .setOnAddButtonClicked(this::openFragmentEditPlan);

        openFragment1();
    }

    private void openFragment1() {
        getSupportFragmentManager().beginTransaction()
                //frameLayout_PlanActivity is the container of both fragments
                .add(R.id.frameLayout_PlanActivity, fragment1)
                .commit();
    }

    public void openFragmentEditPlan(Plan plan) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frameLayout_PlanActivity, FragmentEditPlan.newInstance(plan))
                .addToBackStack("Fragment Edit Plan")
                .commit();
    }
}

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